无法从模式对话框下载,window.showModalDialog
如果使用 window.showModalDialog() 在模式弹出窗口中打开 aspx 页面,我无法从 aspx 页面下载文件。
我在 aspx 页面上有一个图像按钮,单击它会使用一些业务逻辑生成一个 Excel 文件,然后将其添加到响应标头以使该文件可供下载。代码如下所示,
Protected Sub ibtnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExport.Click
...
Some business logic to generate excel file.
...
Response.ClearHeaders()
Response.ContentType = "application/ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=" + someXLSFile )
Response.TransmitFile(someXLSFileWithPath)
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
当我作为模式弹出窗口打开此 aspx 页面时,它不会显示浏览器的下载窗口。在正常情况下(无模式,使用 window.open 打开)弹出下载工作正常。
我还尝试过使用另一种方法来下载文件。我没有在 ibtnExport_Click 中设置响应标头,而是使用 window.open
打开另一个 aspx 页面(例如 Download.aspx),并在 Download 的页面加载事件上设置响应标头.aspx。代码如下所示,
Protected Sub ibtnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExport.Click
...
Some business logic to generate excel file.
...
Session("$FileToDownload$") = someXLSFileWithPath
ClientScript.RegisterStartupScript(GetType(String),"download","window.open('Download.aspx')",true)
End Sub
在 Download.aspx 中,
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim filetoDownload As String = CType(Session("$FileToDownload$"), String)
Dim fileName As String = System.IO.Path.GetFileName(filetoDownload)
Response.ClearHeaders()
Response.ContentType = "application/ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=" + fileName)
Response.TransmitFile(filetoDownload)
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
它适用于模态和非模态弹出窗口,并且在您将应用程序部署到 IIS 上之前会重新启动:)。是的,这种方法适用于 ASP.NET 开发服务器,但不适用于 IIS。
有什么想法可以让下载在模式弹出窗口上工作吗?
I am not able to download a file from a aspx page if aspx page is opened in a modal popup using window.showModalDialog().
I have an image button on aspx page, on click of it a Excel file has been generated by using some business logic and then I add it to the Response header to make that file available for download. The code is as shown below,
Protected Sub ibtnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExport.Click
...
Some business logic to generate excel file.
...
Response.ClearHeaders()
Response.ContentType = "application/ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=" + someXLSFile )
Response.TransmitFile(someXLSFileWithPath)
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
When I open this aspx page as a modal pop up it does not show the browser's download window. In case of normal(modeless, opened using window.open) popup download works fine.
I have also tried using another approach for downloading files. Instead of setting response header in ibtnExport_Click
, I have opened another aspx page, say Download.aspx, using window.open
and set the repsonse headers on page load event of the Download.aspx. The code is as shown below,
Protected Sub ibtnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExport.Click
...
Some business logic to generate excel file.
...
Session("$FileToDownload$") = someXLSFileWithPath
ClientScript.RegisterStartupScript(GetType(String),"download","window.open('Download.aspx')",true)
End Sub
And in Download.aspx,
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim filetoDownload As String = CType(Session("$FileToDownload$"), String)
Dim fileName As String = System.IO.Path.GetFileName(filetoDownload)
Response.ClearHeaders()
Response.ContentType = "application/ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=" + fileName)
Response.TransmitFile(filetoDownload)
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
Well, it works in case of both modal as well as modeless popup and gives a relife until you deploy the application on IIS :). Yes, this approach works on ASP.NET Development server but doesn't work on IIS.
Any ideas, for making download work on modal popup windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是在为此苦苦挣扎。我添加了一个 .ashx 文件来处理代码。这就是我所做的。
这将在模态窗口代码中运行,而不会关闭它或导致错误:
然后在 ProofOfDeliveryDocs.ashx 中添加代码来处理 Response() 内容:(
将 doc.DocumentName 替换为您的文件,我确信您无论如何都想到了这一点)
这是 VB 代码,但如果您使用的话,应该能够很容易地转换为 C# 代码。希望这有帮助!
I was just struggling with this. I added an .ashx file to handle the code. Here is what I did.
This will run in the modal window code without closing it or causing error:
Then add the code in ProofOfDeliveryDocs.ashx to handle the Response() stuff:
(Replace doc.DocumentName with your file, i'm sure you figured that anyway though)
This is VB code but you should be able to translate to C# pretty easily if you are using. Hope this helps!