2010-11-24 121 views
3

aspxページをwindow.showModalDialog()を使用してモーダルポップアップで開くと、aspxページからファイルをダウンロードできません。モーダルダイアログからダウンロードできません。window.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ページ、たとえばDownload.aspxをwindow.openで開き、Download.aspxのページロード時にrepsonseヘッダーを設定しました。以下に示すようにコードは、

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 

でまあ、それは両方のモーダルだけでなく、モードレスポップアップの場合で動作し、アプリケーションをデプロイするまでrelifeを与えますIIS上で:)。はい、このアプローチはASP.NET開発サーバーでは機能しますが、IISでは機能しません。

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

+0

あなたのページにが設定されていると思うので、ハイパーリンクのターゲットを_blankに設定するだけで済みます。 – cstruter

答えて

0

私はちょうどこれで苦労していました。私はコードを処理する.ashxファイルを追加しました。ここに私がしたことがあります。

これを閉じるか、エラーを発生させることなく、モーダルウィンドウのコードで実行されます:

(DOCを置き換えます

Sub DownloadFile() 

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

End Sub 

次に応答()のものを処理するためにProofOfDeliveryDocs.ashxにコードを追加します。あなたのファイルで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#に変換することができなければならないことを考え出した確信しています。お役に立てれば!

関連する問題