gpt4 book ai didi

c# - ASP.NET MVC 下载图像而不是在浏览器中显示

转载 作者:IT王子 更新时间:2023-10-29 04:03:29 25 4
gpt4 key购买 nike

我不想在浏览器窗口中显示 PNG,而是希望操作结果触发文件下载对话框(您知道打开、另存为等)。我可以使用未知内容类型使其与下面的代码一起使用,但用户必须在文件名末尾输入 .png。如何在不强制用户输入文件扩展名的情况下完成此行为?

    public ActionResult DownloadAdTemplate(string pathCode)
{
var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
return base.File(imgPath, "application/unknown");
}

解决方案....

    public ActionResult DownloadAdTemplate(string pathCode)
{
var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
Response.AddHeader("Content-Disposition", "attachment;filename=DealerAdTemplate.png");
Response.WriteFile(imgPath);
Response.End();
return null;
}

最佳答案

我相信您可以使用 content-disposition header 来控制它。

Response.AddHeader(
"Content-Disposition", "attachment; filename=\"filenamehere.png\"");

关于c# - ASP.NET MVC 下载图像而不是在浏览器中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3007652/

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