Nodejs Advanced: File upload based on Express+multer

Source: Internet
Author: User
Tags node server

    • Installing components
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");});
    • Front Page index.html
<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>
    • Running the service
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

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.