如何:jQuery 发布到 ashx 文件以强制下载结果?
我有一个 ashx 处理程序,它从请求中获取“html”属性,然后将其返回给调用它的任何地方。我还将内容处置设置为附件。
直接调用页面可以按预期工作,通过另存为对话框强制下载。
我所坚持的是:我需要从 javascript (jQuery) 执行此操作。我将发布 div 的内容并进行一些进一步的处理。
有人可以给我一些关于如何完成此操作的指示吗?
非常感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会(事实上,做:-))有一个带有隐藏字段的表单,将 div 的内容复制到该字段,然后将表单提交到隐藏的 (
display: none
) iframe 。在页面上包含一个隐藏的 iframe:(
例如,它最初是空白的。我实际上使用了一个
blank.html
文件,这正是它听起来的样子,而不是(比如说)about:blank
因为后者在某些情况下不能正常工作;我不记得细节了。)告诉表单提交到 iframe 并触发您的 ashx 文件:
将 div 的内容复制到字段:
提交表单:(
适当填写各种“...”。)
在我的例子中,我显示一个覆盖 div 告诉用户我在做什么,并启动一个计时器来监视 iframe 的内容(以防止出现错误)消息)和状态 cookie(请参阅 这个答案了解有关 cookie 技巧的更多信息)。根据结果更新或删除叠加层。
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:
(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:
Copying the content of the div to the field:
Submitting the form:
(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.