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?