You've got two options:
make the server tell the browser the file should be downloaded instead of viewed in the browser
To do this, you'd make the server include the HTTP header Content-Disposition: attachment
or, transfer the file data using JavaScript, and then save it to disk using JavaScript
Once your JavaScript has acquired the data, you'd create an <a download="DESIRED_FILENAME">
, construct an ObjectURL using the data and stuff it into the href
, then programmatically click the anchor
The first option is by far the easiest and yields excellent results across all browsers and platforms. The only major drawback is that it requires making code changes on the server. The change is typically small, although sometimes extant code is a hot mess and so even easy things are hard.
The second option works great for desktop browsers but yields absolutely terrible results for almost all mobile browsers. And it might require code changes to the server -- I don't know offhand whether the data can be transferred in its normal binary or whether it would need to be encoded for transport using something like base64. (I've only done this with base64-encoded data, because that's what my server supported.) I also suspect implementation could be a significant challenge for a developer who is unfamiliar with JavaScript and doesn't know how to do their own research.
It might be possible to avoid the UX drawbacks of the second option by using a WebWorker to supply the Content-Disposition header, but I've not tried it.