Node Multer unexpected field

后端 未结 11 1560
情歌与酒
情歌与酒 2020-12-12 12:17

I\'m working on uploading a file to my app using the multer npm module.

The multer function I have defined is to allow a single file uploaded to the file system. Ev

11条回答
  •  忘掉有多难
    2020-12-12 13:08

    This for the Api you could use

     const express        = require('express');
     const bodyParser     = require('body-parser');
     const app = express();
     var multer = require('multer');
     const port = 8000;
     app.use(bodyParser.json());
     app.use(bodyParser.urlencoded({ extended: true }));
    
     app.listen(port, ()=>{
     console.log('We are live on' + port);
     });
    
     var upload = multer({dest:'./upload/'});
    
     app.post('/post', upload.single('file'), function(req, res) {
      console.log(req.file);
     res.send("file saved on server");
     });
    

    This also works fine used on Postman but the file doesn't comes with .jpg extension any Advice? As commented below

    This is the default feature of multer if uploads file with no extension, however, provides you the the file object, using which you can update the extension of the file.

    var filename = req.file.filename; 
    var mimetype = req.file.mimetype; 
    mimetype = mimetype.split("/"); 
    var filetype = mimetype[1]; 
    var old_file = configUploading.settings.rootPathTmp+filename; 
    var new_file = configUploading.settings.rootPathTmp+filename+'.'+filetype; 
    rname(old_file,new_file);
    

提交回复
热议问题