Ajax works with node js multer to implement file upload, nodemulter

Source: Internet
Author: User

Ajax works with node js multer to implement file upload, nodemulter

Description

As a node beginner, I recently worked on a chat software, supports functions such as registration, logon, online single-user, multi-user chat, sending of emotices, uploading and downloading of various files, adding and deleting friends, saving chat records, switching of notification sound, switching of background images, and games, therefore, the multer module is used. After reading various documents and using demo examples, the single file upload function is successfully implemented, and most file formats can be uploaded and displayed on the webpage.

Effect

Is there a visual sense? Yes, it is based on the web version,

To achieve the overall effect, we need to work with css and html. For Beginners at the front end, it is really urgent to post a blog for the first time. In the near future, we will put the code on github, if you are interested, you can check it out.

The code below is light abuse

Configuration

Install

Install multer directly in the cmd command window

npm install multer -save 

Server code

// Introduce httpconst http = require ("http"); // introduce expressconst express = require ("express"); // introduce multerconst multer = require ("multer "); // create a server and bind the listening port var app = express (); var server = http. createServer (app); server. listen (8081); // create a public folder, place the HTML file in it, and allow access to the app. use (express. static ("public"); // Code required for file upload // sets the File Upload path and file name var storage = multer. diskStorage ({destination: function (req, file, cb) {// After the file is uploaded successfully, it will be placed in the public upload folder cb (Null ,'. /public/upload')}, filename: function (req, file, cb) {// set the file name to its original name. You can also add other characters, to distinguish the same file, such as file. originalname + new Date (). getTime (); uses time to distinguish cb (null, file. originalname) }}); var upload = multer ({storage: storage}); // process ajax requests from the page. Single file upload app. post ('/upload', upload. single ('file'), function (req, res, next) {// The network path of the spliced file after upload, var url = 'HTTP: // '+ req. headers. host + '/upload/' + req. file. originalname; // send it back to the client res. json ({code: 1, data: url}); res. end ();});

Client code

<! DOCTYPE html> 

Summary

The above section describes how to use Ajax with node js multer to upload files. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.