2

I'm looking to initiate a download in a webpage using either JavaScript or PHP. All of the examples I've seen use an iframe to load the source of the file. Unfortunately this method doesn't seem to work with all smartphone browsers, specifically my Samsung Galaxy S3. It seems to be a known issue with some of Android's stock browsers.

I cannot use a link because the download must happen auto-magically, and using document.ready or window.load to initiate a window.location call is not acceptable because even though the download initiates it stops all the JS functionality on the current page. I have some JavaScript that must run after the download has initiated.

Basically the flow is:

  1. Show JavaScript progress bar to simulate download progress
  2. Initiate download in background
  3. Once progress bar finishes -> jquery.show new content on page (installation instructions)

Can't seem to find a cross-browser solution that will let me get through all 3 steps above.

Matt K
  • 6,620
  • 3
  • 38
  • 60
  • Is the webpage you're requesting in the same domain as the loading page? If so, I'd use the ajax function in jQuery and load the html into a `div` or something with `$.html`. – Osmium USA Mar 21 '14 at 16:43
  • @OsmiumUSA that doesn't explain how to initiate the download. – Matt K Mar 21 '14 at 16:46
  • Your best bet is probably `$.get`. Read the docs on that function to see if that works for you. – Osmium USA Mar 21 '14 at 18:54
  • @OsmiumUSA Unfortunately it's not possible to use that method, it's a limitation of JavaScript due to security concerns most likely. – Matt K Mar 21 '14 at 19:34
  • Does that mean you're going cross domain here or have I missed something? – Osmium USA Mar 21 '14 at 19:48
  • @OsmiumUSA No it's not cross-domain. You're talking about an AJAX call. How would JavaScript initiate a file download through an AJAX call? There is no way I know of how to do that. – Matt K Mar 21 '14 at 20:06
  • Oh oh I see I see I misread the question. You can still do this with the ajax call but I think there's a special header you can use when responding to the request on the server side that tells the browser the file should be saved, not displayed. – Osmium USA Mar 21 '14 at 20:10
  • Maybe try `Content-Disposition:...` or another answer found here: http://stackoverflow.com/questions/386845/http-headers-for-file-downloads – Osmium USA Mar 21 '14 at 20:13
  • @OsmiumUSA it doesn't matter the content disposition in this case, in what way would I respond to the caller even if the headers are set properly? And once it returned how would I handle it? It just doesn't work out with JavaScript. – Matt K Apr 04 '14 at 16:05

0 Answers0