Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node/Multer Get Filename

Tags:

node.js

multer

I am using the following to upload files to a directory via Multer. It works great, but I need to perform some actions after upload that require the name of the file I just posted to the "upload" directory. How do I get the name of the file I just posted?

// Multer storage options
var storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, 'upload/');
  },
  filename: function(req, file, cb) {
    cb(null, file.originalname + '-' + Date.now() + '.pdf');
  }
});

var upload = multer({ storage: storage });

app.post('/multer', upload.single('file'), function(req, res) {
  // Need full filename created here
});
like image 301
Kode Avatar asked Dec 17 '15 07:12

Kode


6 Answers

var express=require("express");
var app=express();
var multer=require("multer");
var upload=multer({dest:"uploads/"});
app.post("/multer", upload.single("file"), function(req,res){
    console.log(req.file.filename);
});
like image 174
Yash Bele Avatar answered Oct 31 '22 13:10

Yash Bele


request.file gives the following stats, from which you would just need to pick request.file.originalname or request.file.filename to get the new filename created by nodejs app.

{ 
  fieldname: 'songUpload',
  originalname: '04. Stairway To Heaven - Led Zeppelin.mp3',
  encoding: '7bit',
  mimetype: 'audio/mp3',
  destination: './uploads',
  filename: 'songUpload-1476677312011',
  path: 'uploads/songUpload-1476677312011',
  size: 14058414 
}

Eg, in nodejs express mvc app with ecma-6,

var Express = require('express');
var app = Express();

var multipartUpload = Multer({storage: Multer.diskStorage({
    destination: function (req, file, callback) { callback(null, './uploads');},
    filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now());}})
}).single('songUpload');

app.post('/artists', multipartUpload, (req, resp) => {
     val originalFileName = req.file.originalname
     console.log(originalFileName)
}
like image 34
prayagupa Avatar answered Oct 31 '22 13:10

prayagupa


Accessing uploaded files data differs in Multer, depending whether you are uploading single or multiple files. Access data like so:

uploading single file:

req.file

uploading multiple files:

req.files
like image 12
Paul Walczewski Avatar answered Oct 31 '22 12:10

Paul Walczewski


I found the answer on github, you have access to it in res.req.file.filename See there for more informations https://github.com/expressjs/multer/issues/302

like image 3
Ajeddig Faris Avatar answered Oct 31 '22 13:10

Ajeddig Faris


    app.post('/multer', upload.single('file'), function(req, res) {
  // Need full filename created here
const file = req.file
if (!file) {
    const error = new Error('Please upload a file')
    error.httpStatusCode = 400
    return next(error)
  }
 res.send(file) #Here
});

You need recover file from this line

 res.send(file)

using file.filename This output sample

{
  "fieldname": "myFile",
  "originalname": "isc te esta esperando.jpg",
  "encoding": "7bit",
  "mimetype": "image/jpeg",
  "destination": "uploads",
  "filename": "myFile-1602293858948.eaf",
  "path": "uploads/myFile-1602293858948.eaf",
  "size": 297720
} 
like image 2
Beowulfdgo Avatar answered Oct 31 '22 12:10

Beowulfdgo


using request.file.filename

fieldname Field name specified in the form
originalname Name of the file on the user's computer encoding Encoding type of the file
mimetype Mime type of the file
size Size of the file in bytes

like image 1
Ahmed Alassafy Avatar answered Oct 31 '22 13:10

Ahmed Alassafy