如何从MVC控制器提供要下载的文件?
在WebForms中,我通常会有这样的代码让浏览器显示一个“下载文件”弹出窗口,其中包含任意文件类型,如PDF和文件名:
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中完成相同的任务?
慕少森