如何:将jQuery发布到ashx文件以强制下载结果? [英] How to: jQuery post to ashx file to force download of result?

查看:92
本文介绍了如何:将jQuery发布到ashx文件以强制下载结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ashx处理程序,该处理程序从请求中获取'html'属性,然后将其返回给调用它的对象.我也将Content-Disposition设置为附件.

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.

我所坚持的是:我需要使用javascript(jQuery)来做到这一点.我将发布div的内容并进行一些进一步的处理.

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?

非常感谢.

推荐答案

我会(实际上是做:-))的表单带有隐藏字段,将div的内容复制到该字段,然后提交形成隐藏的(display: none)iframe.

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.

在页面上包含隐藏的iframe:

Including a hidden iframe on the page:

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

(例如,它最初是空白的.实际上,我实际上使用的是blank.html文件,它听起来确实像这样,而不是(例如)about:blank,因为在某些情况下后者不能完全正确地工作;我不记不起来了.)

(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.)

告诉表单提交到iframe并触发您的ashx文件:

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

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

将div的内容复制到字段中

Copying the content of the div to the field:

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

提交表格:

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

(适当填写各种"...".)

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

在我的情况下,我显示了一个覆盖div来告诉用户我在做什么,并启动一个计时器,该计时器监视iframe的内容(错误消息)和状态cookie(请参阅

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.

这篇关于如何:将jQuery发布到ashx文件以强制下载结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆