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

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

问题描述

而不是在浏览器窗口中显示PNG,我想动作的结果触发文件下载对话框(你知道打开,另存为等)。我能得到这个下面使用未知内容类型code工作,但然后,用户可以在文件名末尾输入为.png。我怎样才能做到这一点的行为,而不强迫用户在文件扩展名键入?

 公众的ActionResult DownloadAdTemplate(字符串路径code)
    {
        VAR imgPath =使用Server.Mappath(service.GetTemplatePath(路径code));
        返回base.File(imgPath,应用/未知);
    }

解决方案....

 公众的ActionResult DownloadAdTemplate(字符串路径code)
    {
        VAR imgPath =使用Server.Mappath(service.GetTemplatePath(路径code));
        Response.AddHeader(内容处置,附件;文件名= DealerAdTemplate.png);
        Response.WriteFile(imgPath);
        到Response.End();
        返回null;
    }


解决方案

我相信你可以与内容disposition头进行控制。

  Response.AddHeader(
       内容处置,附件;文件名= \\filenamehere.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");
    }

Solution....

    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;
    }

解决方案

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天全站免登陆