Skip to content

Commit

Permalink
Refs #2, Adding filename
Browse files Browse the repository at this point in the history
  • Loading branch information
doumdi committed Mar 26, 2024
1 parent b49cc5a commit d29efb2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions static/file_download.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ function fileDownloadFromBrowser(jsonDocumentString)
"url": "full url with params",
"method": "GET/POST",
"headers": Object with key-value pairs
"filename": "original file name"
}
*/


try {
//Convert string to JSON object
var jsonDocument = JSON.parse(jsonDocumentString);

// do something
console.log("fileDownloadFromBrowser() called with", jsonDocument);

//Create AJAX request with the given method, url and headers
var xhr = new XMLHttpRequest();
xhr.open(jsonDocument.method, jsonDocument.url, true); //async
Expand All @@ -31,7 +35,7 @@ function fileDownloadFromBrowser(jsonDocumentString)
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = "downloaded_file";
a.download = jsonDocument.filename;
a.click();
window.URL.revokeObjectURL(url);
}
Expand All @@ -43,8 +47,6 @@ function fileDownloadFromBrowser(jsonDocumentString)
//Send the request
xhr.send();

// do something
console.log("fileDownloadFromBrowser() called with", jsonDocument);
}
catch (e) {
console.error("fileDownloadFromBrowser() failed with", e);
Expand Down

0 comments on commit d29efb2

Please sign in to comment.