Renaming an uploaded file using Multer doesn't work (Express.js)

后端 未结 6 1706
孤独总比滥情好
孤独总比滥情好 2021-02-07 20:43

I\'m trying to upload a file from a HTML form using Express.js and Multer. I\'ve managed to save the file to the desired location (a folder named uploads).

Howe

相关标签:
6条回答
  • 2021-02-07 21:26

    I know this post is dated. I want to contribute to those who may arrive later. Below is a full functional server script to handle multiple uploaded pictures with random saved pictures names and file extension.

    var express = require("express");
    var multer = require("multer");
    var app = express();
    var path = require("path");
    var uuid = require("uuid");
    
    // Allow cross origin resource sharing (CORS) within our application
    app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });
    
    var storage = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, 'uploadedimages/')
      },
      filename: function (req, file, cb) {
        cb(null, uuid.v4() + path.extname(file.originalname));
      }
    })
    
    var upload = multer({ storage: storage })
    
    // "files" should be the same name as what's coming from the field name on the client side.
    app.post("/upload", upload.array("files", 12), function(req, res) {
        res.send(req.files);
        console.log("files = ", req.files);
    });
    
    var server = app.listen(3000, function() {
        console.log("Listening on port %s...", server.address().port);
    });
    
    0 讨论(0)
  • 2021-02-07 21:29

    The usage for Multer has changed.

    Currently Multer constructor accepts only three options:

    1. dist/storage
    2. fileFilter
    3. limits

    now rename, onFileUploadStart, onFileUploadComplete would not work.

    however renaming can be done using DiskStorage

    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, '/tmp/my-uploads')
        },
        filename: function (req, file, cb) {
            cb(null, file.fieldname + '-' + Date.now())
      }
    })
    
    var upload = multer({ storage: storage })
    

    have a look at these links:

    • https://github.com/expressjs/multer
    • multer callbacks not working ?
    0 讨论(0)
  • 2021-02-07 21:32

    Personally I implemented the following solutions, which generates a random name for files and appends the original file extension (I assume that my extension is after the last . )

    var path = require('path');
    
        var options = multer.diskStorage({ destination : 'uploads/' ,
          filename: function (req, file, cb) {
            cb(null, (Math.random().toString(36)+'00000000000000000').slice(2, 10) + Date.now() + path.extname(file.originalname));
          }
        });
    
        var upload= multer({ storage: options });
    
        router.post('/cards', upload.fields([{ name: 'file1', maxCount: 1 }, { name: 'file2', maxCount: 1 }]), function(req, res, next) {
        /*
          handle files here
          req.files['file1']; //First File
          req.files['file2']; //Second File
          req.body.fieldNames;//Other Fields in the form
    
        */
        });
    


    In the MULTER documentation you'll find this:

    The disk storage engine gives you full control on storing files to disk.

    There are two options available, destination and filename. They are both functions that determine where the file should be stored.

    Note: You are responsible for creating the directory when providing destination as a function. When passing a string, multer will make sure that the directory is created for you.

    filename is used to determine what the file should be named inside the folder. If no filename is given, each file will be given a random name that doesn't include any file extension.

    Note: Multer will not append any file extension for you, your function should return a filename complete with an file extension.

    0 讨论(0)
  • 2021-02-07 21:33

    we give a random name to file with the help of date and appends the original file extension with help of file.mimetype

    try console.log(file.mimetype) you will get the file name and extension separated by '/' then I split it to array and fetch the extension from it. Try the below code.

    let storage = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, './uploads')
      },
      filename: function (req, file, cb) {
        let extArray = file.mimetype.split("/");
        let extension = extArray[extArray.length - 1];
        cb(null, file.fieldname + '-' + Date.now()+ '.' +extension)
      }
    })
    const upload = multer({ storage: storage })
    
    0 讨论(0)
  • 2021-02-07 21:41

    File has structure like this:

    {
    "fieldname": "avatar",
    "originalname": "somefile.pdf",
    "encoding": "7bit",
    "mimetype": "application/pdf",
    "destination": "./uploads",
    "filename": "36db44e11b83f4513188f649ff445a2f",
    "path": "uploads\\36db44e11b83f4513188f649ff445a2f",
    "size": 1277191
    

    }

    The next example saves file with it's original name an extension and not with the strange name like it is by default. (Instead of "file.originalname" you can save it as you want)

    var storage = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, './uploads') //Destination folder
      },
      filename: function (req, file, cb) {
        cb(null, file.originalname) //File name after saving
      }
    })
    
    var upload = multer({ storage: storage })
    
    0 讨论(0)
  • 2021-02-07 21:43

    try this way which i'm using

      var storage = multer.diskStorage({
        destination: function (req, file, cb) {
          cb(null, 'uploads/')
        },
        filename: function (req, file, cb) {
          console.log(file);
          var fileObj = {
            "image/png": ".png",
            "image/jpeg": ".jpeg",
            "image/jpg": ".jpg"
          };
          if (fileObj[file.mimetype] == undefined) {
            cb(new Error("file format not valid"));
          } else {
            cb(null, file.fieldname + '-' + Date.now() + fileObj[file.mimetype])
          }
        }
      })
    
      var upload = multer({ storage: storage })
    
    0 讨论(0)
提交回复
热议问题