简体   繁体   中英

Retrieving image from database not showing up on page

I have been able to upload an image to sql server, in binary format. 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...

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.

I only used response.binarywrite is because that is what I was shown to do. 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.

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!

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.

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.

See this : Retrieve image from database and display on ASP.NET without httphandler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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