简体   繁体   中英

Node js getting name of the file being uploaded

 var express =   require("express");  
    var multer  =   require('multer'); 
    const exec = require('child_process').exec;   
    var app =   express();  
    var storage =   multer.diskStorage({  
      destination: function (req, file, callback) {  
        callback(null, './uploads');  
      },  
      filename: function (req, file, callback) {  
        callback(null, file.originalname);  
      }  
    });  
    var upload = multer({ storage : storage}).single('myfile');  
    debugger;  
    app.get('/',function(req,res){  
          res.sendFile(__dirname + "/index.html");  
    });  
      
    app.post('/uploadjavatpoint',function(req,res){  
        upload(req,res,function(err) {  
            if(err) {  
                return res.end("Error uploading file.");  
            }  
            console.log("Uploading "+res.files);
            res.end("File is uploaded successfully!");  
            console.log("Coverting to PDF");
            debugger;
            exec('"./px-8-5-4-win-x86-64/sdk/demo/pxsample.exe" "./uploads/EM_spectrum.ppt" "./uploads/EM_spectrum.pdf"');
            console.log("Coversion Done");
        });  
    });  
      
    app.listen(2000,function(){  
        console.log("Server is running on port 2000");  
    });

  

I am sending the file using postman with file(myfile) embedded in the body. I want to get the original name of the file being uploaded. I am new to node js please help me how to get the original name of the file being uploaded.

I tried req.file.filename as well as req.files but they don't seem to work

this should help you

>  const _files = req.files;
>  for (const file of _files) {
>       name = file.originalname;
>       size = file.size;
>  }

in postman,

Choose form-data in the body, file in the key and select the file in the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM