2012-01-31 181 views
4

從電子郵件中的鏈接點擊後,下面的代碼會導致一個PDF文檔在瀏覽器中打開:在Adobe Reader中打開PDF,而不是在瀏覽器

Response.ContentType = mime; 
Response.WriteFile(path); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 

有沒有什麼辦法來強迫客戶端在Adobe Acrobat/reader中本地打開它?

+2

我相信,在瀏覽器中打開VS在Adobe Reader中打開一個瀏覽器/用戶設定。你不可能通過C#來解決這個問題。 – 2012-01-31 16:25:16

+0

HTTP規範允許您指定正在發送的二進制信息的***類型***,但無法指定特定的應用程序來處理內容。充其量,您可以強制用戶下載PDF並手動打開該應用程序,該應用程序應在其原生PDF閱讀器中打開,對於許多人來說,該閱讀器是Adobe Acrobat/Reader。 – 2012-01-31 16:36:48

回答

5

客戶端的行爲方式取決於多種因素,包括客戶端的設置...你可以試試這個

Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment; filename="+filePath); 
Response.WriteFile(path); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
+0

是的,它做到了!那麼,無論如何,在一些瀏覽器。其他人的工作方式完全相反! – CompanyDroneFromSector7G 2012-01-31 17:22:20

1

你能做的就是增加一個內容處置頭的最好的事情。這應該會導致出現下載文件對話框。

Response.AddHeader("content-disposition", "attachment;filename=xxx.pdf"); 
2
Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.pdf"); 
Response.TransmitFile(Server.MapPath("~/images/sailbig.pdf")); 
Response.End(); 

這裏的一些好的信息,以幫助:Downloading a File with a Save As Dialog in ASPNETc# dynamically rename file upon download requestHandling file downloads using ASP.NET MVC如果你使用MVC

+0

謝謝你有用的信息。 – CompanyDroneFromSector7G 2012-01-31 17:29:04

相關問題