Using JavaScript I need to download a binary file in the background, then modify such binary data (some internal processing) and at the end to offer a client to save such file to a local drive.
Please advise what kind of approach should I use.
Using JavaScript I need to download a binary file in the background, then modify such binary data (some internal processing) and at the end to offer a client to save such file to a local drive.
Please advise what kind of approach should I use.
If you want to process the data at client-side you can Base64 encode your data at the end and construct an A-element and provide a data-uri
with MIME-type set for the href
attribute.
To download the data you can use AJAX (either with jQuery or directly if jQuery is not an option). Showing jQuery example here:
$.ajax({
url: "pageThatProvidesBinaryData.html",
context: document.body
}).done(function(data) {
processData(data);
});
(be aware of encodings when you deal with binary data).
Process the data, then you can use (html5):
<a href="data:my/mime;charset=uft-8;base64,<your-data>" download="Filename.ext">
Click to download</a>
Replace my/mime
to actual types (ie. application/octet-stream
for generic binaries).
Providing a download
attribute for the A
-tag will allow the user to download the data when link is clicked (if he uses a html5 enabled browser).
If you want this to happen automatically, you can hide the a-element and generate a click:
Is it possible to trigger a link's (or any element's) click event through JavaScript?
For details on data-uris:
https://en.wikipedia.org/wiki/Data_URI_scheme