Displaying image from database... (Copy from ASP. NET forums)

Source: Internet
Author: User
Tags response write
Running on ASP. NET 2.0 and SQL Server 2005. have a table with an image-type column to store a photo. I have already created the script to upload/store the file in the DB, But I need code to handle the output.

I already have a stored proc call the retrieves the data based on an ID... the recordset retrieved is one row with the photo and a few other text fields.

Please advise the necessary code to place in the codebehind file to display this photo. I am using <asp: Label> tag for the positioning of the image.

Thanks!
-------------------------------------------
Re: displaying image from database... excellent... however, I am not storing the content type or file size in the DB... just the image file. I have this code below that is sending me the file, but there is no image... although the size of the file is the same as the original one uploaded. any ideas?

System. configuration. Configuration rootwebconfig =
System. Web. configuration. webconfigurationmanager. openwebconfiguration ("/mysite ");
Connstring = rootwebconfig. connectionstrings. connectionstrings ["strdatasource"];

Sqlconnection objconnection = new sqlconnection (connstring. connectionstring );
Sqldataadapter objcommand = new sqldataadapter ("selectprofile", objconnection );
Dataset objdataset = new dataset ();

Objconnection. open ();
Objcommand. selectcommand. commandtype = commandtype. storedprocedure;
Objcommand. selectcommand. Parameters. Add (New sqlparameter ("@ user_id", sqldbtype. Char, 50 ));
Objcommand. selectcommand. Parameters ["@ user_id"]. value = request. Params ["ID"];
Objcommand. Fill (objdataset, "useraccount ");

Byte [] filedata = (byte []) objdataset. Tables ["useraccount"]. Rows [0] ["photo"];
Int filesize = filedata. length;

Response. clearcontent ();
Response. clearheaders ();
Response. Clear ();
Response. Buffer = false;
Response. contenttype = "image/JPEG ";
Response. addheader ("content-disposition", "attachment; filename=myphoto.jpg ");
Response. addheader ("Content-Length", filesize. tostring ());
Response. binarywrite (filedata );

Objconnection. Close ();
Response. End ();

Re: displaying image from database...

Here are some code bits I have used in the past to get binary data from SQL Server, but I only know how to response write the data as the full output for a page and then reference it in the HTML, I wocould try a searching on Google for actually binding binary image data to the page (not sure if it can be done), I seroiusly doubt that you will be able to bind to an ASP: label

Here is how I have done this in the past
HTML in An ASPX page where you want to show an image from the database

Then you create a getimage. ASPX page that responses GIF or JPG data

// Get data from dbfiletitle = Rs ["filetitle"]. tostring (); filesize = Rs ["filesize"]. tostring (); contenttype = Rs ["contenttype"]. tostring (); byte [] filedata = (byte []) RS ["filedata"]; // response outputresponse. clearcontent (); response. clearheaders (); response. clear (); response. buffer = false; response. contenttype = contenttype; response. addheader ("content-disposition", "attachment; filename =" + filetitle); response. addheader ("Content-Length", filesize. tostring (); response. binarywrite (filedata );

------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.