I have one requirement to download the file from the server using nodejs.
Below is my code to download the file.
test.js
function downloadFile() {
var fsModule = 'fs';
var fs = require(fsModule);
var request = require('request');
var deferred = $q.defer();
var downloadRequest = request.get({
encoding: null,
url: 'https://test.com/api/assets/35007/raw',
headers: {
'Authorization': 'Basic AuthorizationValue'
}
});
downloadRequest.addListener('response', function (response) {
if (response.statusCode != 200) {
console.log('Error in downloading file');
deferred.resolve(false);
} else {
var options = {defaultEncoding: 'binary'};
var writeStream = fs.createWriteStream('c:/testDownloadFile.mp4', options);
response.addListener('data', function (chunk) {
console.log('chunk length - ',chunk.length);
writeStream.write(chunk);
});
response.addListener("end", function() {
writeStream.end();
console.log('No error in download and resolved true');
deferred.resolve(true);
});
}
});
}
downloadFile();
I can run successfully this code using node by below command and it gives response in chunks. so there is no memory issue for this code.
node test.js
but when I run this code in browser using browserify then it doesn't give response in chunks. So it creates a problem for big size file.
- Is there any problem in browserify or request/buffer module?
- What should be the root cause for this issue?
- Could you please provide me the solution or alternate way to download the file in chunks?
Node Version: 6.9.2
browserify: ^12.0.0
Just a brief suggestion what what it's worth... In your testing environment did you have any modules held in cache that are not used in when testing in browser. I made this suggestion as there is no require for the q module.
Regards