views:

52

answers:

1

Hi,

I have an ashx handler which takes the 'html' property from the request, then returns that back to whatever called it. I also have the Content-Disposition set as attachment.

Calling the page directly works as expected, forcing a download with a save as dialog.

What I'm stuck on is this: I need to do this from javascript (jQuery). I'll be posting the content of a div and do some further processing.

Could someone give me some pointers on how this is done?

Many thanks.

+1  A: 

I would (in fact, do :-) ) have a form with a hidden field, copy the content of the div to the field, and then submit the form to a hidden (display: none) iframe.

Including a hidden iframe on the page:

<iframe name="formTarget" src="blank.html" style="display: none">

(E.g., it's initially blank. I actually literally use a blank.html file which is exactly what it sounds like, instead of (say) about:blank because the latter doesn't works quite correctly in some cases; I don't recall the details.)

Telling the form to submit to the iframe and trigger your ashx file:

<form ... action="your.ashx" target="formTarget" ... >

Copying the content of the div to the field:

$("#fieldId").val($("#divId").html()); // Either .html() or .text(), depending on what you want

Submitting the form:

$("#formId").submit();

(Fill in the various "..."s appropriately.)

In my case, I show an overlay div telling the user what I'm doing and start a timer that watches for the content of the iframe (for error messages) and for a status cookie (see this answer for more about the cookie trick). The overlay gets updated or removed depending on the result.

T.J. Crowder
Where the target of the iframe is a page which will then call the ashx? Otherwise the handler is run as soon as the iframe is rendered.
Paul
Thanks for the post, however I'm still unsure how to actually implement this?
Paul
@Paul: No, the ashx would be the action of the form. For the initial src of the iframe, I usually use "blank.html" which is exactly what it sounds like (because about:blank doesn't quite work on some browsers). So you start out with the hidden iframe being (effectively) blank, then submit the data to the ashx file targeting that frame, which should then trigger the download .
T.J. Crowder
I've updated the answer to clarify that a bit.
T.J. Crowder
Doesn't seem to work for me..html: <iframe name="formTarget" src="blank.html" style="display: none"/> <form target="formTarget" action="Excel.ashx" id="formToGetExcel"> <input type="hidden" id="hiddenHtml" /> </form>jQuery: var str = $("<table><tr><td>one one</td><td>one two</td></tr><tr><td>two one</td><td>two two</td></tr></table>"); $("#hiddenHtml").val( $(str).html() ); $("#formToGetExcel").submit();Nothing happens at all. Any further ideas? Thanks
Paul
@Paul: I'd walk through that with a debugger to find out at what stage it's failing. (Side note -- and this shouldn't be the problem -- but `$("#hiddenHtml").val( $(str).html() );` is calling `$` on something [`str`] that's already a jQuery object.) Does it set the hidden value correctly? Does it submit the form? Etc.
T.J. Crowder
Using a submit input on the form posts back to iteself. It's an aspx page so already has the main form on it. Another reason I'd prefer to be using MVC as opposed to regular webforms...Maybe that's the issue. Is there a method for doing this working around this issue?Many thanks for the help.
Paul
@Paul: *"Using a submit input on the form posts back to iteself."* A form submits to the location specified by the `action` attribute on the `form` element, and the results are shown in the window named by the `target` attribute on the `form` element (the same window the form is in being used if there is no `target` attribute, and a new window being created if none matches the given name). I'm not sure I know what you mean by it "submits back to itself."
T.J. Crowder
I've worked around this by having the form in blank.html, and using jQuery set both set the value and submit the form.Many thanks for your help with this.
Paul
@Paul: I'm glad you've got that working, but you really shouldn't have to do that. I don't have to in my main app that does this, with a very similar setup (aspx instead of ashx, and Prototype rather than jQuery, but I don't think either of those matters). V. strange.
T.J. Crowder