[英]How to get the name of this file using formidable (NodeJs)
我有一個使用 nodejs + express 的表單
玉文件:
form(method="post", action="/upload", enctype="multipart/form-data")
input(type="file" name="image")
button(type="submit") Upload
Index.js 並非所有代碼只是必需的
var formidable = require('formidable'),
http = require('http'),
util = require('util');
var fs = require('fs');
MyProject.post('/upload',function(req, res){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files)
{
console.log(files);
});
當我使用 console.log(files) 結果是這樣的
{ iProducto:
{ domain: null,
_events: {},
_maxListeners: 10,
size: 1262294,
path: 'C:\\DOCUME~1\\ADMINI~1\\CONFIG~1\\Temp\\556696bb1c0c6a54362b746c4445
45ca',
name: 'Dibujo.bmp',
type: 'image/bmp',
hash: null,
lastModifiedDate: Mon Jun 09 2014 21:25:42 GMT-0100 (Hora estándar de Cabo
Verde),
_writeStream:
{ _writableState: [Object],
writable: true,
domain: null,
_events: {},
_maxListeners: 10,
path: 'C:\\DOCUME~1\\ADMINI~1\\CONFIG~1\\Temp\\556696bb1c0c6a54362b746c4
44545ca',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
pos: undefined,
bytesWritten: 1262294,
closed: true } } }
現在可以了,但我需要文件名,如果我使用 console.log(files.name) 結果是未定義的,我如何獲得名稱值
您可以通過字段名稱訪問文件名: files.iProducto.name
。
不確定上面的答案是否已過時,但以下內容獲取正在上傳的文件的文件名。
files.file.name
下面可以用來獲取文件的文件名,被formidable重命名后:
var path = require("path");
const name = path.basename(files.file.path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.