casperjs-better-download
v1.0.5
Published
Download a file in the PhantomJS browser context instead of using the built-in CasperJS .download() method.
Downloads
2,429
Maintainers
Readme
casperjs-better-download
Download a file in the PhantomJS browser context instead of using the built-in CasperJS .download()
method. This library uses an async ajax request to circumvent download timeout issues.
Note: despite technically being an async call, this library will block CasperJS step execution.
Why
Many people are having issues with incorrectly downloaded files in CasperJS, resulting in 0kb downloads. This library uses an alternative method of downloading to avoid this issue.
Documentation
Basic use:
var casper = require('casper').create({});
var betterDownload = require('casperjs-better-download');
betterDownload({
casper: casper,
url: 'https://example.com/file.pdf',
targetFilepath: 'data/file.pdf'
});
Using POST:
var casper = require('casper').create({});
var betterDownload = require('casperjs-better-download');
betterDownload({
casper: casper,
url: 'https://example.com/file.pdf',
targetFilepath: 'data/file.pdf',
method: 'POST',
data: 'var1=value&var2=value2'
});
Callbacks (optional):
betterDownload({
...
onComplete: function(fileContent) {
casper.then(function() {
// Do something after file downloads
});
},
onError: function(err) {
console.log(err.message);
console.log('Error data:', JSON.stringify(err.data));
}
});
Timeout (optional):
The default timeout is set to 5 minutes, but you can set it to whatever you want by passing in a timeout value in milliseconds.
var tenMins = 60000 * 10;
betterDownload({
...
waitTimeout: tenMins
});
Credit
This library is just a slight refactoring of laggingreflex's suggestion here.