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

标签 asp.net download modal-dialog self showmodaldialog

如果使用 window.showModalDialog() 在模态弹出窗口中打开 aspx 页面,我将无法从 aspx 页面下载文件。

我在 aspx 页面上有一个图像按钮,单击它会使用一些业务逻辑生成一个 Excel 文件,然后我将其添加到响应 header 以使该文件可供下载。代码如下所示,

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 中设置响应 header ,而是使用 window.open 打开另一个 aspx 页面(例如 Download.aspx),并在 Download 的页面加载事件上设置响应 header .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。

对于在模态弹出窗口上进行下载有什么想法吗?

最佳答案

我只是在为此苦苦挣扎。我添加了一个 .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# 代码。希望这有帮助!

关于asp.net - 无法从模式对话框下载,window.showModalDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4264321/

相关文章:

c# - 创建用户时出错 - ASP.NET System.Web.Providers

flutter | Dio Package ...在后台下载大文件

php - 无法输出多维数组

jsf - 从托管 Bean 函数调用 Primefaces 对话框

wpf - 强制MessageBox在.net/WPF中的应用程序窗口顶部

asp.net - 部署另一个应用程序后无法定位 CodeDom 提供程序

java - 如何使用 Pkcs12 keystore 证书使用 WSDL Web 服务

android - 进度条未在线程 android 中更新

javascript - 带有 Bootstrap 模态的 Mixitup 过滤器

javascript - 在 ASP.NET 代码隐藏中访问客户端修改的 HTML