2012-01-13 111 views
2

我创建了一个ASP.NET/C#应用程序将图像上传到MySQL数据库。该过程执行时没有任何错误,但对于我上传的任何图像,我得到的是100x100白色图像的输出。我遵循的程序是。从数据库中存储和检索图像

1)创建了一个数据库,其中字段picture和类型Binary(255)

2)上传的图像作为cmd.Parameters.Add("@picture", OdbcType.Binary, 255).Value = FileUpload1.FileBytes;

3)上述被插入一个新的记录,并产生以下类型的有价值的东西做。 89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2

// ---------------上传模块完成----------------- //

1 )创建一个ASPX页面

<asp:Image ID="img" runat="server" ImageUrl="~/MyImage.ashx" />

2)左ASPX.CS文件,而无需任何代码

3)增加了一个ASHX文件,

<%@ WebHandler Language="C#" Class="MyImage" %> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Drawing.Imaging; 

public class MyImage : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "image/png"; 
     var data = "89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2"; 
     var buffer = StringToByteArray(data); 
     context.Response.OutputStream.Write(buffer, 0, buffer.Length); 
    } 

    private byte[] StringToByteArray(string hex) 
    { 
     return Enumerable 
      .Range(0, hex.Length) 
      .Where(x => x % 2 == 0) 
      .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) 
      .ToArray(); 
    } 
    public bool IsReusable 
    { 
     get { return false; } 
    } 
} 

通过执行此代码,任何彩色输入的输出中都会显示一个白色的100x100图像。我复选了ContentType并上传了各种尺寸和格式的各种图像,但无论我做什么,我都得到了相同白色图像的输出。我的代码有什么问题?任何更正?

+1

不是在MySQL的专家,但你创建与255bytes的最大长度列?因此最大图像大小确实是255字节? – kolin 2012-01-13 11:42:25

+0

MySQL不允许超过255的二进制文件的长度 – 2012-01-13 13:10:25

回答

1

您可以尝试将图像保存为Base64编码的字符串,然后在您希望在网页上呈现图像时将其转换回图像。

下面是如何做到这一点的链接。

Base64String to Image and visa versa

这里是一个link如何,我用它在我的项目,不完全从图像或从数据库,但概念是相同的。

在使用我的方法在自己的网页,你会使用

<img src="${downloadurl}" /> 
+0

这比编写大量代码要好。谢谢哥们! – 2012-01-13 16:53:54

+0

不客气!祝一切顺利! – 2012-01-14 14:12:54

0

我在拉的图像数据,并从MSSQL数据库

public void ProcessRequest(HttpContext context) 
    { 
     Guid picid; 
     if (context.Request.QueryString["picid"] != null) 
      picid = new Guid(context.Request.QueryString["picid"]); 
     else 
      throw new ArgumentException("No parameter specified"); 

     context.Response.ContentType = "image/jpeg"; 
     Stream strm = ShowPicImage(picid); 
     byte[] buffer = new byte[4096]; 
     int byteSeq = strm.Read(buffer, 0, 4096); 

     while (byteSeq > 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, byteSeq); 
      byteSeq = strm.Read(buffer, 0, 4096); 
     } 
    } 

    public Stream ShowPicImage(Guid piciddata) 
    { 
     ProductPicture pictureData = new ProductPicture("ProductPictureID", piciddata); 
     object img = pictureData.Data; 
     try 
     { 
      return new MemoryStream((byte[])img); 
     } 
     catch 
     { 
      return null; 
     } 
    } 

我不知道,如果它的东西,你可以尝试为您的问题显示出来一个ASHX这种代码。 (凡pictureData.Data属性类型是一个byte []和相关的数据库列是VARBINARY(MAX))

更新答案

您可能还需要考虑存储在数据库作为数据一个BLOB

0

一个255字节的二进制字段类型不会足够大,除了最小的图像之外的任何东西。检查您正在上传的文件的大小,并相应地重新调整字段的大小。

相关问题