1

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

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

OHH It works! thanks I don't know why I didn't try this!
1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.