I am currently working on a project where I need to retrieve some files from a repository. These files are provided through a FileStream and the goal is to enable immediate downloading following a request. The server enforces the download using an application/octet-stream rule.
<form id="myForm1" action="http://fakepath/repository" target="iframe">
<input type="hidden" name="id" value="bP0QjyW5Rmf5yZWNslO0jxNbPg2zXCNLGCTl4bIlhfqQsUxyJ2lFsVimEn1CDQYN">
<button type="submit">DOWNLOAD</button>
</form>
<iframe frameborder="0" id="iframe"></iframe>
This approach works effectively for public files, however, issues arise when attempting to download private files - in such cases, an authorization token must be provided. Unfortunately, HTML forms do not allow setting headers during submission. While an AJAX call would offer more flexibility by allowing custom headers, there is uncertainty around directing the response to a target like with a form. Although handling the response within a callback is possible, it only involves displaying the binary content of the file rather than initiating a download.
With that in mind, I have the following inquiries: - Can AJAX replicate the form’s behavior by automatically targeting an Iframe upon receiving a response to trigger a file download? - Are there alternative methods in JavaScript for managing FileStreams that support header customization?