I was able to write JavaScript to force the browser to download the file from a remote server using the following code:
var iframe = document.createElement("iframe"); iframe.style.display = "none"; iframe.src = "filename.zip" document.body.appendChild(iframe);
Which works great. However, now I have a different situation when the contents of the file are stored in a string in my JavaScript on the browser side, and I need to cause the download of this file. I tried replacing the third line above, where "myFileContents" is the line containing the actual bytes of the file:
iframe.src = "data:application/octet-stream;base64," + Base64.encode(myFileContents);
This loads the file, but the file name is lost. In Chrome, the file name is just βloadingβ. I also read that in some versions of the browser there are restrictions on the file size.
Is there any way to achieve this? Using jQuery will be fine. The solution should support any type of file - zip, pdf, csv, png, jpg, xls, etc.
Sean N.
source share