File uploading with Express 4.0: req.files undefined

后端 未结 9 1760
感动是毒
感动是毒 2020-11-22 10:10

I\'m attempting to get a simple file upload mechanism working with Express 4.0 but I keep getting undefined for req.files in the app.post

相关标签:
9条回答
  • 2020-11-22 10:52

    Please use below code

    app.use(fileUpload());
    
    0 讨论(0)
  • 2020-11-22 10:56

    1) Make sure that your file is really sent from the client side. For example you can check it in Chrome Console: screenshot

    2) Here is the basic example of NodeJS backend:

    const express = require('express');
    const fileUpload = require('express-fileupload');
    const app = express();
    
    app.use(fileUpload()); // Don't forget this line!
    
    app.post('/upload', function(req, res) {
       console.log(req.files);
       res.send('UPLOADED!!!');
    });
    
    0 讨论(0)
  • 2020-11-22 10:57

    PROBLEM SOLVED !!!!!!!

    Turns out the storage function DID NOT run even once. because i had to include app.use(upload) as upload = multer({storage}).single('file');

     let storage = multer.diskStorage({
            destination: function (req, file, cb) {
                cb(null, './storage')
              },
              filename: function (req, file, cb) {
                console.log(file) // this didn't print anything out so i assumed it was never excuted
                cb(null, file.fieldname + '-' + Date.now())
              }
        });
    
        const upload = multer({storage}).single('file');
    
    0 讨论(0)
  • 2020-11-22 10:58

    Here is what i found googling around:

    var fileupload = require("express-fileupload");
    app.use(fileupload());
    

    Which is pretty simple mechanism for uploads

    app.post("/upload", function(req, res)
    {
        var file;
    
        if(!req.files)
        {
            res.send("File was not found");
            return;
        }
    
        file = req.files.FormFieldName;  // here is the field name of the form
    
        res.send("File Uploaded");
    
    
    });
    
    0 讨论(0)
  • 2020-11-22 10:59

    Just to add to answers above, you can streamline the use of express-fileupload to just a single route that needs it, instead of adding it to the every route.

    let fileupload = require("express-fileupload");
    
    ...
    
    app.post("/upload", fileupload, function(req, res){
    
    ...
    
    });
    
    0 讨论(0)
  • 2020-11-22 11:06

    multer is a middleware which handles “multipart/form-data” and magically & makes the uploaded files and form data available to us in request as request.files and request.body.

    installing multer :- npm install multer --save

    in .html file:-

    <form method="post" enctype="multipart/form-data" action="/upload">
        <input type="hidden" name="msgtype" value="2"/>
        <input type="file" name="avatar" />
        <input type="submit" value="Upload" />
    </form>
    

    in .js file:-

    var express = require('express');
    var multer = require('multer');
    var app = express();
    var server = require('http').createServer(app);
    var port = process.env.PORT || 3000;
    var upload = multer({ dest: 'uploads/' });
    
    app.use(function (req, res, next) {
      console.log(req.files); // JSON Object
      next();
    });
    
    server.listen(port, function () {
      console.log('Server successfully running at:-', port);
    });
    
    app.get('/', function(req, res) {
      res.sendFile(__dirname + '/public/file-upload.html');
    })
    
    app.post('/upload', upload.single('avatar'),  function(req, res) {
      console.log(req.files); // JSON Object
    });
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题