简体   繁体   English

从数据库检索图像未显示在页面上

[英]Retrieving image from database not showing up on page

I have been able to upload an image to sql server, in binary format. 我已经能够以二进制格式将图像上传到sql服务器。 This is the code that I am using to upload it.. 这是我用来上传它的代码。

protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string filename = FileUpload1.FileName;

            Stream imgStream = FileUpload1.PostedFile.InputStream;
            int imgLen = FileUpload1.PostedFile.ContentLength;
            byte[] imgBinaryData = new byte[imgLen];

            string strCm = "s_PictInsert";
            SqlCommand cm = new SqlCommand(strCm, Cn);
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.Add(new SqlParameter("@TheImage", SqlDbType.Image)).Value = imgBinaryData;



            Cn.Open();
            cm.ExecuteNonQuery();
            Cn.Close();
        }
    }

When I look at the database, all I see in the database, of the pic I uploaded, is 0x00000000000000 etc...I don't know why it looks like that... 当我查看数据库时,我在数据库中看到的我上传的所有图片都是0x00000000000000等...我不知道为什么看起来像这样...

Then I am using this code to retrieve the image.. 然后,我使用此代码来检索图像。

protected void Button3_Click(object sender, EventArgs e)
    {
        string strCm = "select TheImage from [Pictures] where PictureID = 2";
        SqlCommand cm = new SqlCommand(strCm, Cn);
        cm.CommandType = CommandType.Text;

        Cn.Open();
        SqlDataReader dr = cm.ExecuteReader();
        if (dr.Read())
        {
            Response.ContentType = "image/jpg";
            Response.BinaryWrite((byte[])dr["TheImage"]);
        }
        Cn.Close();
    }

when I click the button it doesn't show me the image it just writes the markup out to the page. 当我单击按钮时,它没有显示图像,只是将标记写到页面上。

What I want to do is retrieve the image and put it to an asp image, the thing is the images that I upload can be .png format. 我想要做的就是检索图像并将其放入asp图像,问题是我上传的图像可以是.png格式。

I only used response.binarywrite is because that is what I was shown to do. 我仅使用response.binarywrite是因为这是我被证明要做的。 But it doesn't work the way I was told it would. 但这并不能像我被告知的那样起作用。

Thanks 谢谢

The problem is with your upload function, you are not setting imgBinaryData with the data from the file stream, you are only settings the size to match, so it will default to all zeros. 问题在于您的上传功能,您没有将imgBinaryData与文件流中的数据一起设置,仅将大小设置为匹配,因此它将默认为全零。

Instead of this part: 代替这一部分:

byte[] imgBinaryData = new byte[imgLen];

Replace it with this: 替换为:

byte[] imgBinaryData = null;
using (var reader = new BinaryReader(FileUpload1.PostedFile.InputStream))
{
    imgBinaryData = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);
}

Then check to see what your database data looks like. 然后检查一下数据库数据。

On a side note, I would recommend always storing file name and MIME type when storing files in a database, you never know when you will need them! 附带一提,我建议在将文件存储在数据库中时始终存储文件名和MIME类型,而您永远不知道何时需要它们!

try this: 尝试这个:

var length = FileUpload1.PostedFile.ContentLength;
Byte[] imgBinaryData = new Byte[length];
var stream = FileUpload1.FileContent;
stream.Read(imgBinaryData, 0, length);

then insert the binary object. 然后插入二进制对象。 Your code does not read the image into the stream for insertion. 您的代码不会将图像读取到流中进行插入。 The above code should read the image into the byte array, then your db code should insert actual data instead of the placeholder. 上面的代码应将图像读入字节数组,然后您的db代码应插入实际数据而不是占位符。

You are storing image in binary format to the database, you will retrieve the same. 您正在将图像以二进制格式存储到数据库,您将检索相同的图像。 You need to use MemoryStream to get the image from the byte array that you retrieved. 您需要使用MemoryStream从检索到的字节数组中获取图像。

See this : Retrieve image from database and display on ASP.NET without httphandler 参见: 从数据库检索图像并显示在没有httphandler的ASP.NET上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM