HTML OnSubmit: Download OR HTML
I have a situation here: A HTML form page, on submit returns Excel/PDF/Word document based on POST values, if NOT returns an Error HTML.
With respect to friendly UI, wants to download if everythi开发者_运维技巧ng is fine OR show an error dialog WITHOUT NAVIGATING TO error page. Download works fine. But in case of HTML it navigates based on the response.
Other details:
- Backend set content-type to "application/pdf" or whatever
- Using jQuery/UI dialog in the front end.
- Using jQuery form submit:
$("#form").submit();
Any help?
The way I handle this is:
- Have the form submit to a hidden
iframe
. - The form includes a hidden field with the name of a cookie the response should set (on success or failure).
- I show a nice message like "Generating your report" or whatever and poll for the cookie.
- If the generation is successful, I'll see the cookie with the value "OK" (or similar); this tells me to take down my "generating" message and stop polling.
- If the generation fails, I'll see the cookie with the value "Error" (or similar); this tells me to read the full error message from the contents of the
iframe
and show the error to the user. - I use a timeout (but a long one, five minutes or something) and also let the user cancel the polling.
- When returning the PDF, I use a
Content-Disposition
header totellsuggest to the browser that it should offer to save the result (valueattachment
) or show it inline (valueinline
).
精彩评论