代码之家  ›  专栏  ›  技术社区  ›  Vinod T. Patil

无法从模式对话框、窗口下载。showModalDialog

  •  3
  • Vinod T. Patil  · 技术社区  · 15 年前

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

    我在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 ,我已经打开了另一个aspx页面,比如下载。aspx,使用 window.open 并在下载的页面加载事件上设置repsonse标头。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
    

    下载中。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。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Taylor Brown    13 年前

    我只是在挣扎。我加了一个。ashx文件处理的代码。这就是我所做的。

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

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

    然后将代码添加到DeliveryDocs的证明中。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。希望这有帮助!

    推荐文章