NPM Install Express Multer--save
- Service-Side Code Server.js
varExpress = require (' Express ');varMulter = require (' Multer '));varBodyparser = require (' Body-parser '));varApp =Express (); App.use (Bodyparser.json ());//Multer provides storage This parameter to personalize the path and file name of the resource save. varStorage =multer.diskstorage ({destination:function(req, file, callback) {callback (NULL, "./images"); }, FileName:function(req, file, callback) {callback (NULL, File.fieldname + "_" + date.now () + "_" +file.originalname); }});//Field name and max count multiple file uploadsvarUpload = Multer ({storage:storage}). Array ("Imguploader", 3); //var upload = Multer ({dest: ' upload/'}); simple configuration//can also be written in the Post method//app.post ('/upload-single ', Upload.single (' imguploader '), function (req, res, next) {//res.send ({ret_code: ' 0 '});// });App.get ("/",function(req, res) {Res.sendfile (__dirname+ "/index.html"); //You can also read files like this //var form = Fs.readfilesync ('./form.html ', {encoding: ' UTF8 '}); //res.send (form);}); App.post ("/api/upload",function(req, res) {upload (req, res,function(err) {if(err) {returnRes.end ("Something went wrong!"); } returnRes.end ("File uploaded sucessfully!.")); });}); App.listen (2000,function(a) {Console.log ("Listening to Port 2000");});
<form id= "Frmuploader" enctype= "Multipart/form-data" action= "api/upload/" method= "POST" > <input type= " File "name=" Imguploader "multiple/> <input type=" file "name=" Imguploader "multiple/> <input type = "File" name= "Imguploader" multiple/> <input type= "Submit" name= "Submit" id= "Btnsubmit" value= "Upload"/ ></form>
Node server
Visit Http://127.0.0.1:2000/, select File, click "Submit", done. Then you will see a file in the images directory.
- File information
Console.log (' File type:%s ', File.mimetype); Console.log (' original file name:%s ', file.originalname); Console.log (' File size:%s ', file.size); Console.log (' File save path:%s ', File.path);
Nodejs Advanced: File upload based on Express+multer