The multer of node. js

Source: Internet
Author: User

A lot of people will follow the node. js actual combat that book together, yes, in general, we will be smooth, but sometimes very depressed, such as Multer.

The author uses 0.1.6, this is already too old, now is the 1.1.0, of course you can choose the old version, but as our programmer, how can stay.

First the error will be

New TypeError ('app.use () requires middleware functions');

This does not matter, we Baidu, this has a reference: http://segmentfault.com/q/1010000003050818?sort=created

I now upgrade Multer 1.1.0, also encountered the same problem, and then go to his official website to understand.

I can't write that anymore.

app.use(multer({dest:‘./‘}));

You're going to write in the route.

var multer =Require Multer '); var upload = Multer ({dest:  '/upload ', Upload.fields ([{name:  ' file1 '}, {name:  ' File3 '}, {name:  File4 '}, {name:  ' File5 '}]),  Function (req, res, next) {for (var I in req.files) {console.log (Req.files[i]);} req.flash ( Span class= "hljs-string" > ' success ',  ' file upload succeeded! '); Res.redirect ( '/upload ');}); 
/span>
Yes, that's what we wrote in Index.js.
varMulter = require ('Multer');varUpload = Multer ({dest:'./public/images/user'}); App.post ('/upload', Upload.fields ([{name:'file1'}, {name:'file2'}, {name:'File3'}, {name:'file4'}, {name:'File5'}]), function (req, res, next) { for(varIinchreq.files)    {Console.log (req.files[i]); } req.flash ('Success','File Upload successful!'); Res.redirect ('/upload');});

Yes, baby, you can upload, but after the upload generated is a randomly generated name, this can not have,

Reference Official Document: Https://github.com/expressjs/multer

var storage=Multer.Diskstorage ({Destination:function (Req, file, CB) { CB (null,  '/tmp/my-uploads')}, filename: function (req, C12>file, CB) { CB (null, file.  FieldName +  '-' + Date.  Now ())}})var upload = multer ({storage: storage})

Then modify the code:
var multer = require (' Multer '), var storage = Multer.diskstorage ({  destination:function (req, file, CB) {    CB (NULL , './public/images/');  },  filename:function (req, file, CB) {    CB (NULL, file.originalname);}  ) var upload = Multer ({storage:storage});  App.post ('/upload ', checklogin);  App.post ('/upload ', upload.fields ([    {name: ' File1 '}, {name: ' File2 '}, {name:    ' File3 '},    {name: ' File4 '},    {name: ' File5 '}  ]), function (req, res, next) {for    (var i in req.files) {      Console.log ( Req.files[i]);    }    Req.flash (' Success ', ' File upload succeeded! ');    Res.redirect ('/upload ');  });

Kiss, you'll be all right,

Version Ah, version, remember, remember.

The multer of node. js

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.