Antoine Guittet
Antoine Guittet

Reputation: 476

Reconstructing a chunked file in Node.js

I am working on a angularjs / nodejs (express) stack. In this project, I have to upload large binary file (~150mb) from the client to server. Due to a company proxy limitation, we have to chunk the files thanks to the ng-file-upload library. The uploaded files are chunked into 1mb requests that look like this :

------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="binServerUrl"

https://server-url.com/upload/
------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="_chunkSize"

1048576
------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="_currentChunkSize"

1048576
------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="_chunkNumber"

2
------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="_totalSize"

5209196
------WebKitFormBoundaryAHupR7ANrwE1zXZR
Content-Disposition: form-data; name="binaryFile"; filename="test.exe"
Content-Type: application/octet-stream

ÎéoÓ¦­«ÅP9`÷Ú­ådFÕ+Oø¾ö³ÎÖ>XÖU§ÚÔadæNËm·\ÄM3U¨êÞû¢éëT¹¸Kµõª/E

However, at the moment, I am not able to handle those received chunks on server side. At the moment, I'm using Multer middleware and when I receive a new chunk, the previous one is overwritten by the new one.

The code of my middleware looks like this :

var multer = require('multer');

module.exports = function(req, res, next) {
  storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, config.filePath + '/' + req.params.binaryId);
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname);
    }
  });
  return multer({ storage: storage }).fields([{ name: 'binaryFile'},{ name: 'manifestFile'}])(req, res, next);
};

I read a lot about node streams thinking it will help me to solve my problem but without really understanding how to deal with.

Can someone have any idea about how reconstruct my files on server side ?

Thanks in advance and excuse my mistake if any.

Upvotes: 0

Views: 753

Answers (0)

Related Questions