在我的应用程序中,我想给用户下载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