Node-restify: req.files['null']

Created on 15 Nov 2016  路  2Comments  路  Source: restify/node-restify

Hi,

I've implemented a file upload with restify using multer before, but since restify is supposed to handle multipart-data by itself now I decided to use this approach. As I'm not sure whether I will support multiple file uploads I decided to implement this behaviour right away and had following issue I was able to fix but unfortunately I'm far off being a JavaScript expert (iOS, Android native) and I'm not sure where the actual problem resides.

I simplified the code as far as possible:
// Configure bodyParser
server.use(restify.bodyParser({ maxBodySize: 0, mapParams: false, mapFiles: false, overrideParams: false, uploadDir: os.tmpdir(), multiples: true, hash: 'sha1' }));

Sending multiple files to my upload function as form-data results in following request data:
console.log(req.files)
{ null:
[ File {
domain: [Object],
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 44595,
path: '/var/folders/zv/456ywhk93c39v792_qwd6lcr0000gp/T/upload_e7327c613646c8b7ee8005928d109e40',
name: 'sampleImage.png',
type: 'image/png',
hash: 'f61da98363360137653f60b44f6e79a1d8d3c574',
lastModifiedDate: Tue Nov 15 2016 11:10:00 GMT+0100 (CET),
_writeStream: [Object] },
File {
domain: [Object],
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 1356,
path: '/var/folders/zv/456ywhk93c39v792_qwd6lcr0000gp/T/upload_c0c157736cbe3c64435052ebbdd3d49d',
name: 'sampleFile',
type: 'application/octet-stream',
hash: 'b80afd9fd0825344853fc6d2bb638a036cf51dc0',
lastModifiedDate: Tue Nov 15 2016 11:10:00 GMT+0100 (CET),
_writeStream: [Object] } ] }

As you might have noticed req.files is an object with the key 'null' so actually I cannot iterate over a files array. But I can access the array using:

var files = req.files['null']

or iterate over the files using underscore for example:
_.each(req.files['null'], function(file) { console.log(file.name) });

So I'm not sure if my setup is wrong or it's an issue with restify bodyParser.

Most helpful comment

@marxcheckito Can you share us your new way ? Thanks

All 2 comments

Problem solved as I used another approach uploading my data

@marxcheckito Can you share us your new way ? Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nodesocket picture nodesocket  路  4Comments

mongris picture mongris  路  5Comments

mikemilano picture mikemilano  路  4Comments

RaghavendhraK picture RaghavendhraK  路  5Comments

anuxs picture anuxs  路  3Comments