【问题标题】:ASP.NET Return image from .aspx linkASP.NET 从 .aspx 链接返回图像
【发布时间】:2010-02-12 16:17:28
【问题描述】:

当用户单击另一个 ASP.NET 页面的链接时,是否可以将图像(或任何文件类型)输出到下载链接?

我有文件名和字节[]。

<a href="getfile.aspx?id=1">Get File</a>

...getfile 返回文件而不是转到 getfile.aspx 页面。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    你真的会想要.ashx for that ;)

    public class ImageHandler : IHttpHandler 
    { 
      public bool IsReusable { get { return true; } } 
    
      public void ProcessRequest(HttpContext ctx) 
      { 
        var myImage = GetImageSomeHow();
        ctx.Response.ContentType = "image/png"; 
        ctx.Response.OutputStream.Write(myImage); 
      } 
    }
    

    【讨论】:

    【解决方案2】:

    How to Create Text Image on the fly with ASP.NET

    类似这样的:

    string Path = Server.MapPath(Request.ApplicationPath + "\image.jpg");
    Bitmap bmp = CreateThumbnail(Path,Size,Size);
    Response.ContentType = "image/jpeg";
    bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
    bmp.Dispose();
    

    【讨论】:

    • ashx 大部分时间都是您想要的,但有时您确实需要一个 aspx 页面,例如,如果您使用不接受 ashx 的 MapPageRoute。
    【解决方案3】:

    这是我过去的做法:

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", string.Format("inline;filename=\"{0}.pdf\"",Guid.NewGuid()));
    Response.ContentType = @"application/pdf";
    Response.WriteFile(path);
    

    【讨论】:

    • "inline;" 之间有一个空格并且“文件名”在我的服务器上似乎很重要
    【解决方案4】:

    是的,你要彻底清除response并用图片字节数据作为字符串替换它,并且你需要确保根据image的类型为content-type设置response header

    【讨论】:

    • 如果我没有内容类型怎么办?
    • 从文件扩展名推断它。这就是操作系统所做的一切(将 jpg 扩展名更改为 gif 并询问任何内置函数或实用程序它是什么类型的文件)。
    • 根据w3.org/Protocols/rfc1341/4_Content-Type.htmlen.wikipedia.org/wiki/Internet_media_type,您应该对未知的内容类型使用“application/octet-stream”。但是,如果您发送要在页面中显示的图像,那么浏览器将必须知道内容类型,并且您必须按照@Rich 所说的去做。但是,如果您只是保存文件的内容,因为浏览器并不关心它是什么,那么八位字节流内容类型将起作用。
    【解决方案5】:

    是的,这是可能的。您需要设置 Response 对象的两个部分:Content-Type 和 HTTP Header。 MSDN documentation 包含有关响应对象的详细信息,但主要概念非常简单。只需将代码设置为类似这样(对于 Word 文档)。

    Response.ContentType="application/ms-word";
    Response.AddHeader("content-disposition", "attachment; filename=download.doc");
    

    还有一个更完整的例子here

    【讨论】:

      【解决方案6】:

      getfile.aspx 的代码隐藏代码必须有一个content-type,浏览器会知道它是图像或未知文件并让您保存它。

      在 asp.net 中,您可以使用Response 对象设置ContentType,即

      Response.ContentType = "image/GIF"
      

      Here你有动态生成图片的教程

      【讨论】:

        【解决方案7】:

        灰...

        public class ImageHandler : IHttpHandler
        {
        
            public void ProcessRequest(HttpContext ctx)
            {
               string path = ".....jpg";
        
                        byte[] imgBytes = File.ReadAllBytes(path);
                        if (imgBytes.Length > 0)
                        {
                            ctx.Response.ContentType = "image/jpeg";
                            ctx.Response.BinaryWrite(imgBytes);
                        }
            }
        
            public bool IsReusable
            {
                get {return false;}
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-09
          • 2015-12-19
          • 2010-10-19
          • 1970-01-01
          • 1970-01-01
          • 2012-02-28
          • 1970-01-01
          相关资源
          最近更新 更多