2

I have a file upload system in sails.js app. I want to process the uploads before saving them in the server. My form on the client side allows multiple file uploads. Now on the server side how do I know how many files were sent?

For example I can find the total bytes to be expected from the upload using the following:

req._fileparser.form.bytesExpected

However, I couldn't find something similar that helps me find the total number of files sent to the server.

Also the above code req._fileparser.form.bytesExpected, is there a better way to get total combined file size of the files sent through the upload form by the client?

pewpewlasers
  • 3,025
  • 4
  • 31
  • 58

1 Answers1

5

In the github repository for Skipper there is a file: index.js

Line 92 from the above file, which appears to deal with multipart file uploads, contains the following:

var hasUpstreams = req._fileparser && req._fileparser.upstreams.length;

You should check the length of upstreams in your code, and see if that contains the number of files you sent.

Another option: send a parameter in your request from the client with the number of files uploaded.

See the skipper ReadMe section about Text Parameters.

Skipper allows you to access the other non-file metadata parameters (e.g "photoCaption" or
"commentId") in the conventional way. That includes url/JSON-encoded HTTP body parameters (req.body), querystring parameters (req.query), or "route" parameters (req.params); in other words, all the standard stuff sent in standard AJAX uploads or HTML form submissions. And helper methods like req.param() and req.allParams() work too.

I've just found a previous question/answer on stackoverflow.

You might try using var upload = req.file('file')._files[0].stream to access and validate, as shown in the above answer.

Community
  • 1
  • 1
beeThree
  • 145
  • 2
  • 7
  • Something similar to your first option is what I am looking for. But `upstreams.length` isn't giving me the right number of files sent for upload. Btw, if you are curious about what I want it for. I want to set max file size dynamically. For example if I allow 10 mb per file, and the user uploads more than 1 file at once, 10mb might not be enough. This is also the reason why I cannot trust the number sent as param by the client. – pewpewlasers Oct 06 '14 at 19:39
  • thanks for the stackoverflow link. It was good help. But I am still unable to get the total number of files received. The `headers` value only shows information about one of the files. – pewpewlasers Oct 07 '14 at 04:44