2

I am currently using the following JS code to trigger a file download without leaving the page I'm on:

var iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.src = "/somefile.zip";

It works well pretty much everywhere I tested except on both the stock Android browser and Dolphin, where the download doesn't start at all. So far so good, after some research this hidden iframe trick happens to be known not to work on the Android browser.

But I tried several other methods to trigger the download on the Android browser, including window.open() (not reliable because popup blocking is enabled by default), or <a target="_blank"> with a simulated click() (which from a popup blocker perspective amounts to window.open() and gets blocked), or document.location = ... which downloads the file but breaks my app.

The problem with the latter document.location = ... is that this is a Comet application (server-push / long polling) so I really can't leave the page I'm currently on (and "leaving" includes changing document.location even for a file download, even if apparently the browser stays on the current page) otherwise the long polling connection is stopped and the updates stop, the app breaks. This obviously also applies when clicking normal links, either manually or simulated.

So in order not to break my app I really need to trigger a file download without leaving the page I'm on. Unfortunately I didn't find any viable solution that also works on the stock Android browser.

Any ideas?

Thanks for reading me.

syam
  • 14,701
  • 3
  • 41
  • 65
  • Does this work with firexox, opera, dolphin,...? – greenapps Mar 24 '13 at 13:00
  • @greenapps Sorry I should have mentioned that. It fails only on stock Android browser and Dolphin. – syam Mar 24 '13 at 14:00
  • Did you try to refresh the page and generate new content each time, it's being refreshed? – ETech Jun 07 '13 at 08:51
  • @GOST I'm not sure I understand you. Where do you suggest I put that ``? In the ` – syam Jun 07 '13 at 09:06

1 Answers1

0

Try using the anchor and simulated click without using a target=blank

I say this because I had a similar download consisting of an iframe and a simple link as fallback. The iframe worked on everything but the android, but the simple link would download successfully without leaving the page.

Relaxing In Cyprus
  • 1,976
  • 19
  • 25
  • Sorry for the long delay, looks like I somehow missed the notification. As I said in my question, I can't use a normal link because even though the browser *appears* to stay on the same page, the Comet long polling connection gets closed and my app breaks. – syam Jun 07 '13 at 09:00