I need help in uploading an image in jquery then afterwards sending it to server-side for its insertion to sql server and retrieving image from sql server then appending it to a div. Is there a way to do this from client-side to server-side and vice versa? I've read so many articles, but most of them are pure server-side. Can anyone give me an example? Any help will be fully appreciated.
Asked
Active
Viewed 5,215 times
1
-
Are you using file upload control to upload the file ? and you want it to save to DB asynchronously ? why are you saving whole image in a DB you should store in a local directory and save path in db – MSUH Jun 27 '12 at 11:56
-
Yes, I will be using a file upload control. My plan is to convert the file into data and store it to the server. As much as possible I don't want to store the imagei in a local directory. I want it to be stored at the DB. – Luke Villanueva Jun 27 '12 at 12:02
1 Answers
0
For Async file upload see This article.
1) By this you would be able to get image bytes which you can save it to db or you can convert it to image and save it to directory.
2) Once you save the image you can get the bytes from the db, in your page you can use
<img id="img" src="GetImage.ashx?ImageId=1"/>
In GetImage.ashx you can write following code
//byte[] image = Get image bytes from DB using ImageID
System.Drawing.Image img = byteArrayToImage(image);
MemoryStream stream = new MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
stream.Dispose();
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(data);
Byte conversion function
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

MSUH
- 872
- 2
- 9
- 20
-
I would recommend using a generic handler (ashx) rather than a webform (aspx). no sense in carrying all the added baggage of webforms only to clear it out and respond with an image – Jason Meckley Jun 27 '12 at 12:28
-
Yes you are right its a typo, i have used handler in my project for handling this. Editing my answer – MSUH Jun 27 '12 at 12:31
-
Can you guys give me an example on how can I process a certain image from the database. I've been practicing retrieving rows and filling it to a datatable. With this solution, how can I pass the retrieved image from the datatabase to the generic handler for the conversion? I'm sorry for this questions since this is my first time handling image uploading/downloading. Thanks! – Luke Villanueva Jun 27 '12 at 14:18
-
@ljpv14 google "asp.net load image from database" and follow any one of the hundreds of examples online. – Jason Meckley Jun 27 '12 at 14:46
-
@MSUH- Hello again, after experementing I found myself trying to use your code for image displaying. I have a question. I have an error at this line: Image returnImage = Image.FromStream(ms); // Parameter not valid. I retrieved the image bytes from the database and stored it at a datatable and convert it to byte[] like this. byte[] image = ((byte[])dt.Rows[0]["UserImage"]);. Can you help me? – Luke Villanueva Jul 12 '12 at 06:54
-
Hi Ijpv14! How are you saving an image ? it should be of Type Image in Database else retrieval will not result in an image and your code crashed. See [this](http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image) – MSUH Jul 13 '12 at 06:37