最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the name of this file using formidable (NodeJs) - Stack Overflow

programmeradmin5浏览0评论

I have a form using nodejs + express

Jade File:

form(method="post", action="/upload", enctype="multipart/form-data")
  input(type="file" name="image")
  button(type="submit") Upload

Index.js not all code just the necessary

var formidable = require('formidable'),
http = require('http'),
util = require('util');
var fs = require('fs');


MyProject.post('/upload',function(req, res){
var form = new formidable.IningForm();
form.parse(req, function(err, fields, files) 
{
  console.log(files);
});

When I use console.log(files) the result is this

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

Now it is ok but I need the file name and if I use console.log(files.name) the result is undefined how can I get the name value

I have a form using nodejs + express

Jade File:

form(method="post", action="/upload", enctype="multipart/form-data")
  input(type="file" name="image")
  button(type="submit") Upload

Index.js not all code just the necessary

var formidable = require('formidable'),
http = require('http'),
util = require('util');
var fs = require('fs');


MyProject.post('/upload',function(req, res){
var form = new formidable.IningForm();
form.parse(req, function(err, fields, files) 
{
  console.log(files);
});

When I use console.log(files) the result is this

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

Now it is ok but I need the file name and if I use console.log(files.name) the result is undefined how can I get the name value

Share Improve this question asked Jun 9, 2014 at 22:30 krlosruizkrlosruiz 1134 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can access the filename by the field name: files.iProducto.name.

Not sure if the above answer is outdated, but the following gets the filename of the file being uploaded.

files.file.name

Below can be used to get the filename of the file, after being renamed by formidable:

var path = require("path");
const name = path.basename(files.file.path)

In the current latest version, you can get the file name by files.file[0].originalFilename and you can get the file path by files.file[0].filepath.

Earlier it was files.file.name and files.file.path

发布评论

评论列表(0)

  1. 暂无评论