node.js 节点/Multer 获取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34328846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:38:48  来源:igfitidea点击:

Node/Multer Get Filename

node.jsmulter

提问by Kode

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 将文件上传到目录。它工作得很好,但我需要在上传后执行一些操作,这些操作需要我刚刚发布到“上传”目录的文件的名称。如何获取我刚刚发布的文件的名称?

// 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
});

回答by prayagupd

request.filegives the following stats, from which you would just need to pick request.file.originalnameor request.file.filenameto get the new filename created by nodejs app.

request.file提供以下统计信息,您只需要从中选择request.file.originalnamerequest.file.filename获取由 nodejs 应用程序创建的新文件名。

{ 
  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,

例如,在 nodejs 中使用 ecma-6 表达 mvc 应用程序,

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)
}

回答by Yash Bele

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);
});

回答by Paul Walczewski

Accessing uploaded files data differs in Multer, depending whether you are uploading singleor multiplefiles. Access data like so:

访问上传​​的文件数据在 Multer 中有所不同,具体取决于您是上传单个文件还是多个文件。像这样访问数据:

uploading single file:

上传单个文件:

req.file

uploading multiple files:

上传多个文件:

req.files

回答by Ahmed Alassafy

using request.file.filename

使用 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

fieldname 以表单形式指定的字段名称
originalname 用户计算机上的文件名称 encoding 文件的编码类型
mimetype 文件
大小的Mime 类型 文件的大小(以字节为单位)