开发者

Unable to download from modal dialog, window.showModalDialog

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
开发者_如何学Python

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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜