tags:

views:

1973

answers:

6

I am looking to fileupload a picture jpeg,gif,etc into an SQL database on an updateprofilepicture page. Then on the profile page, I want to retrieve the image from an sql database and have it show up in an Asp:Image control. I have much code trying to do this and it doesn't work. The table contains a column of type Image.

+2  A: 

The important thing to remember here is that you shouldn't try to transmit the image data with the profile page itself. Instead, you want your profile page to generate HTML markup for the browser that looks something like this:

<img src="~/MyImageHandler.ashx?UserID=1234" alt="User 1234 avatar" width="100px" height="150px" />

That is the ultimate result of your <asp:Image .../> control. Then the browser will send a completely separate Http request to retrieve the image. That's how pictures on web sites work. You then need to be able to handle that additional request. To do that, create an Http handler (*.ashx file) and use it to retrieve the appropriate image data from the database and send it to the browser.

Joel Coehoorn
ok, would you maybe happen to just have an outline of how to go about inserting and storing an image. I know the image gets converted to a binary array....I am pretty sure I can figure out how to insert the image. When it come to retrieving the image I have no idea. How the *.ashx file work?
That's really a different topic: the answer will depend on a number of things not included in this question, like how your database table is set up, what makes the primary key for the table, how security will work, and how your file upload control is set up.
Joel Coehoorn
If you really want, you can edit that information into this question, but you might get more eyes on your question is you open each issue in it's own question.
Joel Coehoorn
+5  A: 

As Joel mentioned you should use an HttpHandler or a page to display the image. Here is a sample code to output image (Image.ashx) :

// ProcessRequest method of Image.ashx
long imageId = Convert.ToInt64(Request.QueryString["ImageId"]);

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
    "SELECT ImageFile FROM ImageTable WHERE ImageId = @ImageID", conn))
{
    command.Parameters.Add("@ImageID", SqlDbType.Int).Value = imageId;
    conn.Open();

    Response.ContentType = "image/gif";
    Response.BinaryWrite((byte[]) command.ExecuteScalar());
}

and then use image in your page as :

  <asp:Image id="Image1" runat="server" ImageUrl="Image.ashx?ImageID=12"/>
Canavar
excellent I will definitely try that and get back. So basically create an "image page" like above and then use the *.ashx page to request the image page? I apologize I am fairly new to asp, I am used to php. Thank you for all of your help.
I think you've got a bit backwards: your *.ashx _is_ the image page. Your regular <asp:Image> control results in a request for the *.ashx page. Also: I cleaned up SG's code a bit to better handle bad sql requests, though this is still pretty rudimentary.
Joel Coehoorn
@Joel Coehoorn : thanks Joel, now it looks perfect :)
Canavar
Thank you so much, I will try that tonight! I am amazed at how fast I received a response. Many thanks!
How exactly do you use a handler page. Will all this code exist inside of the ProcessRequest (HttpContext context) function?
yes, first I write my post to support aspx page. If you prefer handler, you should put your code into the ProcessRequest.
Canavar
I actually prefer an *.aspx because that is what I am familiar with. The Http Page Response does not exist inside the *.ashx page. I think I will try using just a separate *.aspx page.
If you prefer aspx, clear all content in aspx file, in aspx.cs you should put the code inside the load event. and dont forget to change imageUrl property of asp:Image as aspx page.
Canavar
WOW! It finally works, I know this is sad but I have been trying to get this to work for over a week. There is little to no information on this. You guys have been an invaluable resource. Thank you very much. Can the Image.aspx page generate multiple images that I can request say for an album?
+1  A: 

If you're using SQL 2005 or greater you should not use the data type Image because it's now deprecated. Instead you want to use the new Varbinary(MAX) type if possible. Once you have it stored all you need to do is retrieve it via ADO.Net call and cast the cell value into type Byte[] and then call Response.BinaryWrite like in ScarletGarden's example above.

James
+1  A: 

After a few hundred gigabytes of images, I believe you'll find yourself thinking that the operating systems' file system and static file http servers is better suited than the database, which is busy which a lot of other details, for storing images. It also allows you to use thousands of existing free tools to work with, move, host, etc the images.

dlamblin
A: 

Instead of storing images in the database, store the path and/or filename for the image. Images will fill up the database and make it slow.

Fogh