无法从模式对话框下载,window.showModalDialog

发布于 2024-10-04 21:05:33 字数 2022 浏览 7 评论 0原文

如果使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-10-11 21:05:33

我只是在为此苦苦挣扎。我添加了一个 .ashx 文件来处理代码。这就是我所做的。

这将在模态窗口代码中运行,而不会关闭它或导致错误:

Sub DownloadFile()

    'use the ashx handler file to download the file
    Response.Redirect("~/Dispatch/ProofOfDeliveryDocs.ashx?id=" & lstDocuments.SelectedValue)

End Sub

然后在 ProofOfDeliveryDocs.ashx 中添加代码来处理 Response() 内容:(

将 doc.DocumentName 替换为您的文件,我确信您无论如何都想到了这一点)

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim doc As DeliveryDoc = New DeliveryDoc

    If Not context.Request.QueryString("id") = Nothing Then

        doc = doc.GetDeliveryDoc(context.Request.QueryString("id")) 'get the file

        context.Response.Clear()
        context.Response.ContentType = "application/x-unknown"
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & doc.DocumentName)
        context.Response.BinaryWrite(doc.FileData.ToArray)

    End If

End Sub

这是 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:

Sub DownloadFile()

    'use the ashx handler file to download the file
    Response.Redirect("~/Dispatch/ProofOfDeliveryDocs.ashx?id=" & lstDocuments.SelectedValue)

End Sub

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)

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim doc As DeliveryDoc = New DeliveryDoc

    If Not context.Request.QueryString("id") = Nothing Then

        doc = doc.GetDeliveryDoc(context.Request.QueryString("id")) 'get the file

        context.Response.Clear()
        context.Response.ContentType = "application/x-unknown"
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & doc.DocumentName)
        context.Response.BinaryWrite(doc.FileData.ToArray)

    End If

End Sub

This is VB code but you should be able to translate to C# pretty easily if you are using. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文