I have access a website that has a simple button (I dont own the site and dont have access to the source) to download a document. I am using the code below to execute this and it seems to work ok but is glitchy
Objective
I want to download the file
Post the file data to another site
Issues
- Sometimes the document downloaded is large or simply there isnt a document present after clicking the button
- Doesnt work on the same version of FF on different machines same OS
The code below
Components.utils.import("resource://gre/modules/Downloads.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
window.content.location.href = "javascript:void download_document()";
Task.spawn(function () {
let list = yield Downloads.getList(Downloads.ALL);
let downloads = yield list.getAll();
setTimeout(function(d_before){
Task.spawn(function(d_before) {
let list = yield Downloads.getList(Downloads.ALL);
let downloads = yield list.getAll();
var file = downloads[downloads.length-1];
var parts = file.target.path.split('/');
var document_name = parts[parts.length-1];
// alert(document_name);
var file = FileUtils.getFile("DfltDwnld", [document_name]);
Components.utils.import("resource://gre/modules/NetUtil.jsm");
NetUtil.asyncFetch(file, function(inputStream, status) {
// alert("Fetching file");
if (!Components.isSuccessCode(status)) {
return;
}
var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
// alert("Reading file data");
data = window.btoa(data);
// alert("File data read");
// alert(prefs.getCharPref("server_ip"));
xmlhttp.open("POST",ht_server+"/import_document",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("authentication_token="+prefs.getCharPref("api_key")
+"&email="+prefs.getCharPref("email")
+"&body="+encodeURIComponent(content.document.body.innerHTML)
+"&document_name="+document_name
+"&document_data="+encodeURIComponent(data));
// alert("Finished");
});
}).then(null, Components.utils.reportError);
},3000);
}).then(null, Components.utils.reportError);
The code above is not complete for my solution but my main concern is that it does work on some machines and on others I get this error (when a document is downloaded)
NS_ERROR_NOT_AVAILABLE: Async version must be used nsHelperAppDlg.js:209:0
[Exception... "Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsIFile.append]" nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)" location: "JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getFile :: line 43" data: no] Promise-backend.js:873:0
NS_ERROR_NOT_AVAILABLE: Async version must be used nsHelperAppDlg.js:209:0
[Exception... "Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsIFile.append]" nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)" location: "JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getFile :: line 43" data: no] Promise-backend.js:873:0
A promise chain failed to handle a rejection. Did you forget to '.catch', or did you forget to 'return'?
See https://developer.mozilla.org/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
Not being an expert in this I've not been able to resolve it
Can anyone give some advice?