javascriptnode.jsformsexpressformidable

How to get the name of this file using formidable (NodeJs)


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.IncomingForm();
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


Solution

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