[英]open dialog to download pdf
我有一個gridview,其中為用戶提供了下載pdf文件的選項。 當他們單擊pdf圖標時,有時會在新選項卡中打開pdf文件,有時會開始下載。 我如何才能使其始終下載?
您需要添加一個按鈕(圖像按鈕,linknbutton或按鈕)並處理GridView的RowCommand事件。 在RowCommand處理程序中,您可以編寫代碼來下載文件。
您可以使用Response
對象的方法。
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
為了始終強制進行下載,您需要添加AVD所示的Content-Disposition標頭。 但是,我認為這完全沒有必要; 我認為在新窗口中打開PDF鏈接就足夠了。 換句話說,定義了target="_blank"
。 例:
<a href="file.pdf" target="_blank">invoice</a>
然后,由用戶決定是要在本地保存文件還是僅在屏幕上查看文件。 我認為重要的是,這不會干擾用戶正在查看的當前頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.