我怎样才能提交一个文件从MVC控制器下载?
在WebForms中,我通常会有这样的代码让浏览器提供一个任意文件types(如PDF)和文件名“下载文件”popup窗口:
Response.Clear() Response.ClearHeaders() ''# Send the file to the output stream Response.Buffer = True Response.AddHeader("Content-Length", pdfData.Length.ToString()) Response.AddHeader("Content-Disposition", "attachment; filename= " & Server.HtmlEncode(filename)) ''# Set the output stream to the correct content type (PDF). Response.ContentType = "application/pdf" ''# Output the file Response.BinaryWrite(pdfData) ''# Flushing the Response to display the serialized data ''# to the client browser. Response.Flush() Response.End()
我如何在ASP.NET MVC中完成相同的任务?
根据文件是否存在或者您即时创build,从您的操作中返回FileResult
或FileStreamResult
。
public ActionResult GetPdf(string filename) { return File(filename, "application/pdf", Server.UrlEncode(filename)); }
要强制下载PDF文件,而不是由浏览器的PDF插件处理:
public ActionResult DownloadPDF() { return File("~/Content/MyFile.pdf", "application/pdf", "MyRenamedFile.pdf"); }
如果你想让浏览器按默认行为(插件或下载)进行处理,只需发送两个参数即可。
public ActionResult DownloadPDF() { return File("~/Content/MyFile.pdf", "application/pdf"); }
您将需要使用第三个参数为浏览器对话框上的文件指定一个名称。
更新:Charlino是正确的,当传递第三个参数(下载文件名) Content-Disposition: attachment;
被添加到Http响应头。 我的解决scheme是发送application\force-download
作为mimetypes,但是这会产生一个下载文件名的问题,所以第三个参数需要发送一个好的文件名,因此不需要强制下载 。
你可以在剃刀或控制器中做同样的事情。
@{ //do this on the top most of your View, immediately after `using` statement Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=receipt.pdf"); }
或者在控制器中
public ActionResult Receipt() { Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=receipt.pdf"); return View(); }
我在Chrome和IE9中试过,都是下载pdf文件。
我可能应该添加我使用RazorPDF来生成我的PDF文件。 这是一个关于它的博客: http : //nyveldt.com/blog/post/Introducing-RazorPDF
你应该看看Controller的File方法。 这正是它的目的。 它返回一个FilePathResult而不是一个ActionResult。
mgnoonan,
你可以这样做来返回一个FileStream:
/// <summary> /// Creates a new Excel spreadsheet based on a template using the NPOI library. /// The template is changed in memory and a copy of it is sent to /// the user computer through a file stream. /// </summary> /// <returns>Excel report</returns> [AcceptVerbs(HttpVerbs.Post)] public ActionResult NPOICreate() { try { // Opening the Excel template... FileStream fs = new FileStream(Server.MapPath(@"\Content\NPOITemplate.xls"), FileMode.Open, FileAccess.Read); // Getting the complete workbook... HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true); // Getting the worksheet by its name... HSSFSheet sheet = templateWorkbook.GetSheet("Sheet1"); // Getting the row... 0 is the first row. HSSFRow dataRow = sheet.GetRow(4); // Setting the value 77 at row 5 column 1 dataRow.GetCell(0).SetCellValue(77); // Forcing formula recalculation... sheet.ForceFormulaRecalculation = true; MemoryStream ms = new MemoryStream(); // Writing the workbook content to the FileStream... templateWorkbook.Write(ms); TempData["Message"] = "Excel report created successfully!"; // Sending the server processed data back to the user computer... return File(ms.ToArray(), "application/vnd.ms-excel", "NPOINewFile.xls"); } catch(Exception ex) { TempData["Message"] = "Oops! Something went wrong."; return RedirectToAction("NPOI"); } }
使用.ashx文件types并使用相同的代码