ASP.NET MVC 下载图像而不是在浏览器中显示 [英] ASP.NET MVC download image rather than display in browser

查看:28
本文介绍了ASP.NET MVC 下载图像而不是在浏览器中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Rather than displaying a PNG in the browser window, I'd like the action result to trigger the file download dialogue box (you know the open, save as, etc). I can get this to work with the code below using an unknown content type, but the user then has to type in .png at the end of the file name. How can I accomplish this behavior without forcing the user to type in the file extension?

    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 标题来控制这一点.

I believe you can control this with the content-disposition header.

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

这篇关于ASP.NET MVC 下载图像而不是在浏览器中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆