2012-12-18 25 views
8

私はbinariファイル、pdf、word、またはExcelをWebブラウザに送信するページを持っています。 Firefox、およびIEでの両方を使用すると、whant、このファイルを使用して行う「開く」やChromeが直接コンピュータに保存ASP.NETコードの後ろにファイルをダウンロードするときにChromeに「ファイルを開く」ダイアログを開く方法

enter image description here

しかし「保存」するために何をすべきかを尋ねるダイアログが表示されます。

このファイルで何をしたいのですか?、ファイルをブラウザに送信する前にWebレスポンスにメタデータを挿入しますか?

Response.ContentType 
Content-Disposition, application,and 
filename=" + FileName 

ダウンロードを強制します:

答えて

9

保存ダイアログを促すためのChromeを強制することはできません。ユーザーはクロームの設定でこの動作を変更する必要があります(詳細設定)

enter image description here

+0

参考になりますか? –

+0

@ValentinDespaが答えを更新しました。 – daniloquio

+0

しかし、このチェックボックスをチェックするようにユーザーに依頼するのは現実的ではありません。だからこそ他の解決策がありますか? –

0

あなたはブラウザがあなたのようなストリーミングしている事を処理する方法を知っているので、いくつかのヘッダを送信する必要があります。また、詳細については、このリンクを参照してください。

http://www.devtoolshed.com/aspnet-download-file-web-browser

おかげ

0

多分それは、その設定は次のようになり

System.String filePath = "c:\\tempFile.pdf" 
System.IO.FileInfo fileInfo = new FileInfo(filePath); 

System.Web.HttpContext context = System.Web.HttpContext.Current; 
System.Web.HttpResponse response = context.Response; 
response.Clear(); 
response.ClearHeaders(); 
response.ClearContent(); 
response.ContentType = "application/pdf"; 
response.AppendHeader("content-type", "application/pdf"); 
response.AppendHeader("content-length", fileInfo.Length.ToString()); 
response.AppendHeader("content-disposition", String.Format("attachment; filename={0}.pdf", outputFileName)); 
response.TransmitFile(filePath); 
response.Flush(); // this make stream and without it open chrome save dialog 
context.ApplicationInstance.CompleteRequest(); // send headers and c# server-side code still continue 

System.IO.File.Delete(filePath); // clean cache here if you need 

response.End();