【发布时间】:2014-09-30 21:59:54
【问题描述】:
在我的应用程序中,我想为用户提供下载 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>
通过点击“下载”,文件被浏览器打开。
【问题讨论】:
标签: c# asp.net-mvc pdf download