nodejs body-parser file upload form

  1. We need to introduce modules
var Express = require('express');
var multer = require('multer');
var bodyParser = require('body-parser');
  1. first step
var Storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, "./Images");
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
    }
});

destination: upload location profile for the images
I do not know specifically how to do, Baidu did not come out

Filename and the file name
fieldName specified in the form field name + ' ' + + stamp ' name' + files on the user's computer

  1. third step
var upload = multer({ storage: Storage }).array("imgUploader", 3); //Field name and max count

Here multer accept the storage we created in the previous step as a parameter
to accept an array of file length of 3, all file names are imgUploader.
Here Insert Picture Description
This time the front end of the file upload will exist in the way we write in req.files
and other content exist req.body, as with the body-parser usage (there are not the same, req.files deposit is an array )

Here Insert Picture Description

Problems I encountered in use

  1. Use formData obtain contents of the form can only be used when a query such as native js
let form = document.getElementById("test-form")
  1. After finished uploading find background error Unexpected field
    name property input box form to upload files to the front end of the line imgUploader ** (mean provided in front of the third step 'file name' is the same, i.e., the input name attribute name = "file name")**
<input type="file" name = "imgUploader" accept="image/gif, image/jpeg,image/x-png">

Finally, attach a: multer use the link: GitHub multer file example

Published 50 original articles · won praise 23 · views 1252

Guess you like

Origin blog.csdn.net/qq_44698161/article/details/102593041
Recommended