我需要为我的用户提供文件,此文件可以是pdf或xls文件。
我似乎无法找到一种简单的方法。
这就是我所得到的:
public FileResult DownloadPDF()
{
return view("/download/pdf1");
}
答案 0 :(得分:2)
最快的方法是做这样的事情:
public ActionResult DownloadPdf()
{
return File("~/Download/pdf1.pdf", "application/pdf", Server.UrlEncode("NameOfFile.pdf"));
}
答案 1 :(得分:0)
我找到的最好最快的方式:
public void DownloadReport(string path)
{
// Clear the content of the response
Response.ClearContent();
FileInfo newFile = new FileInfo(path);
string fileName = Path.GetFileNameWithoutExtension(newFile.Name) + DateTime.Now + newFile.Extension;
// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
// Add the file size into the response header
Response.AddHeader("Content-Length", newFile.Length.ToString());
// Set the ContentType
Response.ContentType = "application/vnd.ms-excel";
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(newFile.FullName);
// End the response
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
//send statistics to the class
}
将文件的路径传递给此方法&根据您的文件类型更改内容类型。