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
、Download.aspx などの別の aspx ページを開き、Download.aspxwindow.open
のページ読み込みイベントで応答ヘッダーを使用して設定しました。コードは以下のとおりです。
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 では機能しません。
モーダルポップアップウィンドウでダウンロードを機能させるためのアイデアはありますか?