【问题标题】:Serve file in byte[] as URL将 byte[] 中的文件作为 URL 提供
【发布时间】:2016-09-07 11:17:32
【问题描述】:

我正在开发 web 应用程序的服务器端组件,该组件应显示存储在数据库中的图像。

我正在尝试找到一种将字节数组或流转换为 HTML img 标记的有效 URL 的方法。

byte[] 包含整个文件,包括标题。

我一直在寻找解决方案,但一直发现从 url 保存到文件流的反向问题。

有没有办法通过某种动态生成的 url 来提供文件,还是我必须创建文件的物理副本才能链接到?

【问题讨论】:

    标签: c# asp.net url server bytearray


    【解决方案1】:

    您可以将字节数组转换为 Base64 图像。

        public string getBase64Image(byte[] myImage)
        {
            if (myImage!= null)
            {
                return "data:image/jpeg;base64," + Convert.ToBase64String(myImage);
            }
            else
            {
                return string.Empty;
            }
        }
    

    您的图片标签将如下所示:<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgA...">

    或者对于较大的图像(和其他文件类型),最好使用Generic Handler

        public void ProcessRequest(HttpContext context)
        {
            //check if the querystring 'id' exists
            if (context.Request.QueryString["id"] != null)
            {
                string idnr = context.Request.QueryString["id"].ToString();
    
                //check if the id falls withing length parameters
                if (idnr.Length > 0 && idnr.Length < 40)
                {
                    //get the data from the db or other source
                    byte[] bin = getMyDataFromDB();
    
                    //clear the headers
                    context.Response.ClearHeaders();
                    context.Response.ClearContent();
                    context.Response.Clear();
                    context.Response.Buffer = true;
    
                    //if you do not want the images to be cached by the browser remove these 3 lines
                    context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    context.Response.Cache.SetValidUntilExpires(false);
    
                    //set the content type and headers
                    context.Response.ContentType = "image/jpeg";
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\"");
                    context.Response.AddHeader("content-Length", bin.Length.ToString());
    
                    //write the byte array
                    context.Response.OutputStream.Write(bin, 0, bin.Length);
    
                    //cleanup
                    context.Response.Flush();
                    context.Response.Close();
                    context.Response.End();
                }
            }
        }
    

    您的图片标签将如下所示:&lt;img src="/Handler1.ashx?id=AB-1234"&gt;

    【讨论】:

    • 我不知道您实际上可以将图像本身嵌入到 src 标签中!我马上试试。
    • 请注意,转换为 Base64 会创建一个大字符串。对于大于 50 kb 的图像,我通常不会使用第一种方法。
    • @VDWWD 无需创建 ashx 处理程序:您的代码将图像作为 byte[](第二种解决方案)在“正常”getimage.aspx 页面的 Page_Load() 中工作 - 只需替换 @ 987654326@ 和 this.(或没有)
    • 我需要使用 PDF 来执行此操作,所以我想我会尝试这种方法,但它会更像 Meg 而不是 50K。你有什么其他的建议可以提供。谢谢,迈克
    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多