0

Currently, I'm downloading the file using POST form:

var form = document.createElement("form");
var element1 = document.createElement("input");
var element2 = document.createElement("input");

form.target = "_blank";
form.method = "POST";
form.action = path;

element1.value = authService.getToken();
element1.name = "Authorization";
form.appendChild(element1);

element.append(form);

form.submit();
element.empty();

In order to prevent current page's location change when the server doesn't send correct headers, I open set the form's target to "_blank", so that if an error occurs it is shown on the other page. But the problem here is that browsers block new tabs by default, and I don't want to force users to allow such behaviour. I've read that also there an option to specify iframe's id as a target. Is it going to work in my case? How can I then read an error from the iframe to show to a user?

5
  • are you POSTing to your site?
    – dandavis
    Sep 26, 2015 at 5:33
  • Can you be kind enough to search for iframeupload which does exactly what you need..except the part where you allow download it allows upload. Sep 26, 2015 at 5:47
  • @dandavis, yes, I'm using POST Sep 26, 2015 at 5:58
  • you can show the iframe, so that the user can see any error message. even a small "banner ad"-sized rectangle should suffice
    – dandavis
    Sep 26, 2015 at 6:03
  • yeah, that also maybe an option, thanks Sep 26, 2015 at 6:25

1 Answer 1

1

I am working on this very problem right now. The best answer I've found for returning state from the iframe is to set a cookie in it. The cookie's name should ideally be unique to the particular download event (I'm using a guid), known to both pages, and its value can be an error message or empty for success. The parent page then polls for this cookie in javascript. I make sure that the download url always renders the cookie if there's an error, because it's hard to learn anything else about the state of the iframe. On success, the JS poller can hide a ”your download will begin shortly" message, and delete the cookie. On failure, do those and also show the error.

The big unanswered question is how well it'll work in mobile browsers. Popups are a terrible choice with them because they mostly default to blocking them with no prompt... but nevertheless there's a jQuery plugin out there for iframe downloads which, when it sees mobile, falls back to popups. That scares me.

2
  • thanks, that's an interesting approach, can you provide some code examples? Sep 26, 2015 at 5:59
  • Maybe, but not until Monday at the earliest. Sep 26, 2015 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.