在我的應用程序中,我想給用戶下載PDF文件的選項。在我的代碼中,文件被瀏覽器打開;不過,我想要下載文件。這裏是我的代碼:用C#下載PDF文件
控制器
string name = id; //id is the name of the file
string contentType = "application/pdf";
var files = objData.GetFiles(); //list of files
string filename = (from f in files
orderby f.DateEncrypted descending
where f.FileName == name
select f.FilePath).First(); //gets the location of the file
string FullName = (from f in files
where f.FileName == name
select f.FileName).First(); //gets the new id in new location to save the file with that name
//Parameters to File are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(filename, contentType, FullName);
這裏是我如何下拉菜單中使用它。
查看:
<li><a id="copyURL" href="@Url.Action("Download", "Home", new { id = item.FileName})">Download</a></li>
通過點擊 「下載」,該文件被打開的瀏覽器。
瀏覽器仍然嘗試打開它。我用Chrome和IE試了一下。 – user3853986 2014-09-30 22:24:27
你可以嘗試在你的return語句之前添加這行嗎? 'Response.AddHeader(「content-disposition」,「attachment; filename =」+ filename);' – 2014-09-30 22:45:51
仍然一樣。我試過 Response.AddHeader(「content-disposition」,「attachment; filename =」+ filename); 和 Response.AddHeader(「content-disposition」,「attachment; filename =」+ FullName); – user3853986 2014-09-30 23:01:43