gpt4 book ai didi

asp.net-mvc - 如何呈现文件以供从 MVC Controller 下载?

转载 作者:行者123 更新时间:2023-12-03 04:43:17 25 4
gpt4 key购买 nike

在 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 中完成相同的任务?

最佳答案

返回FileResultFileStreamResult来自您的操作,具体取决于文件是否存在或您即时创建。

public ActionResult GetPdf(string filename)
{
return File(filename, "application/pdf", Server.UrlEncode(filename));
}

关于asp.net-mvc - 如何呈现文件以供从 MVC Controller 下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/730699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com