页面渲染和 http 处理程序
我们有一个在新浏览器窗口中打开的页面,其中自动重定向到生成某种 XLS 文件的 ASHX 处理程序。这是使用 JavaScript 重定向完成的,即将 window.location.href
设置为 ASSX 的 URL。
尽管它可以工作并显示文件的下载对话框,但设置 window.location.href 也会清除窗口的内容,使其保持空白。这在某种程度上是有道理的,但在前台打开下载对话框时保留上一页的内容仍然会很好。是否有可能以某种方式(通过推迟重定向的执行或使用不同的技术来调用 ASHX 处理程序)?
另一件好事是,如果我们可以在显示下载对话框后关闭父页面,这可以以任何方式工作吗?
We have a page that opens in a new browser window where there's an automatic redirect to an ASHX handler that produces some kind of an XLS file. It's done using a javascript redirect, i.e. setting window.location.href
to the URL of the ASHX.
Although it works and presents the download dialog for the file, setting window.location.href also clears the content of the window so that it stays blank. It somehow makes sense but still it would be nice to keep the content of the previous page there while opening the download dialog in the foreground. Is it possible somehow (by defering the execution of the redirect or using a different technique to call the ASHX handler) ?
Another nice to have thing would be if we could close the parent page after the download dialog is presented, could this work in any way ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态创建(在 javascript 中)一个指向下载 ashx 位置的隐藏 iframe 并将其添加到 DOM 就可以了。
Dynamically creating (in javascript) an hidden iframe that points to the download ashx location and adding it to the DOM would do the trick.
通过在处理程序的响应中使用内容处置标头,您可以显示保存对话框,而无需打开新窗口。您不需要使用 javascript 打开新窗口或创建 iframe。
请参阅此问题了解可能的 Excel MIME 类型 ( contentType )
设置 Excel 文档的 mime 类型
By using the content-disposition header in your response from your handler you can display the save dialog without having to open a new window. You won't need to use javascript to open a new window or create an iframe.
See this question for the possible excel mime types ( contentType )
Setting mime type for excel document