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

发布于 2024-09-04 12:57:09 字数 720 浏览 5 评论 0原文

我不想在浏览器窗口中显示 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;
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

衣神在巴黎 2024-09-11 12:57:09

我相信您可以使用内容处置标头来控制它。

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

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

Response.AddHeader(
       "Content-Disposition", "attachment; filename=\"filenamehere.png\""); 
遇见了你 2024-09-11 12:57:09

您需要在响应中设置以下标头:

  • Content-Disposition: Attachment; filename="myfile.png"
  • 内容类型:application/force-download

You need to set the following headers on the response:

  • Content-Disposition: attachment; filename="myfile.png"
  • Content-Type: application/force-download
画尸师 2024-09-11 12:57:09

我实际上来这里是因为我正在寻找相反的效果。

    public ActionResult ViewFile()
    {
        string contentType = "Image/jpeg";



        byte[] data = this.FileServer("FileLocation");

        if (data == null)
        {
            return this.Content("No picture for this program.");
        }

        return File(data, contentType, img + ".jpg");
    }

I actually came here because I was looking for the opposite effect.

    public ActionResult ViewFile()
    {
        string contentType = "Image/jpeg";



        byte[] data = this.FileServer("FileLocation");

        if (data == null)
        {
            return this.Content("No picture for this program.");
        }

        return File(data, contentType, img + ".jpg");
    }
那一片橙海, 2024-09-11 12:57:09

对于 MVC,我使用 FileResult 并返回 文件路径结果

public FileResult ImageDownload(int id)
    {
        var image = context.Images.Find(id);
        var imgPath = Server.MapPath(image.FilePath);
        return File(imgPath, "image/jpeg", image.FileName);
    }

With MVC I use a FileResult and return a FilePathResult

public FileResult ImageDownload(int id)
    {
        var image = context.Images.Find(id);
        var imgPath = Server.MapPath(image.FilePath);
        return File(imgPath, "image/jpeg", image.FileName);
    }
此岸叶落 2024-09-11 12:57:09

在您的情况下下载文件的正确方法是使用 FileResult 类。

 public FileResult DownloadFile(string id)
{
try
{
    byte[] imageBytes =  ANY IMAGE SOURCE (PNG)
    MemoryStream ms = new MemoryStream(imageBytes);
    var image = System.Drawing.Image.FromStream(ms);
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    var fileName = string.Format("{0}.png", "ANY GENERIC FILE NAME");
    return File(ms.ToArray(), "image/png", fileName);
}
catch (Exception)
{
}
return null;
}

The correct way to download file in your case is to use FileResult class.

 public FileResult DownloadFile(string id)
{
try
{
    byte[] imageBytes =  ANY IMAGE SOURCE (PNG)
    MemoryStream ms = new MemoryStream(imageBytes);
    var image = System.Drawing.Image.FromStream(ms);
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    var fileName = string.Format("{0}.png", "ANY GENERIC FILE NAME");
    return File(ms.ToArray(), "image/png", fileName);
}
catch (Exception)
{
}
return null;
}
笨死的猪 2024-09-11 12:57:09

实际上,我@7072k3

var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;

从我的工作代码中复制了它。
这仍然使用标准的 ActionResult 返回类型。

This I actually @7072k3

var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;

Copied that from my working code.
This still uses the standard ActionResult return type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文