3

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 では機能しません。

モーダルポップアップウィンドウでダウンロードを機能させるためのアイデアはありますか?

4

1 に答える 1

0

私はこれに苦労していました。コードを処理するために .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# を使用している場合は、かなり簡単に変換できるはずです。お役に立てれば!

于 2012-12-28T16:30:58.987 に答える