【问题标题】:ASP.NET MVC download image rather than display in browserASP.NET MVC 下载图像而不是在浏览器中显示
【发布时间】:2011-03-01 18:17:13
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net-mvc image download controller


    【解决方案1】:

    在您的情况下下载文件的正确方法是使用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;
    }
    

    【讨论】:

      【解决方案2】:

      对于 MVC,我使用 FileResult 并返回 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);
          }
      

      【讨论】:

        【解决方案3】:

        这个我其实@7072k3

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

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

          • 嘿,我也是!事实证明,我们只需要删除文件名,以便您的代码如下所示: return File(data, contentType)
          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

            • Content-Disposition: attachment; filename="myfile.png"
            • Content-Type: application/force-download

            【讨论】:

            • 谢谢。需要内容类型才能强制下载为文件。
            • content-type 是一个很好的解决方案,内容处理也可以,但是你可能会遇到一些风险,请存在另一篇关于它的帖子stackoverflow.com/a/1012461/2062838
            猜你喜欢
            • 2011-06-12
            • 2011-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-18
            • 2019-05-07
            • 2016-07-31
            • 1970-01-01
            相关资源
            最近更新 更多