1

I'm doing an intranet for the employees of the company using ASP.NET and SQL Server, on the profile section i have an upload picture area (image field to show the picture, input to select the file, and the load button), this is working fine, the picture is loading into a folder, but, also i have the text fields where the user adds his info, this is also working great and is saving into the database, what now i want to do is that the user uploads the picture, and finish with all the text fields when he clicks the submit button to send all the fields into the database the name or the path of the picture he uploaded goes into the database too, all my textfields are being saved using an sqlDataSource with insertParameters.

Any idea how to do it?? i would prefer to do it with the front side code, since on the code behind i just have initializing the SqlDataSource1.Insert();

Ivelisse
  • 69
  • 8
  • please could you show what have you implemented to handle the image that comes from the user to be saved on the server? – Francisco Spaeth Jul 09 '12 at 20:10
  • This question has been asked numerous times before, e.g. [Store pictures as files or in the database for a web app?](http://stackoverflow.com/questions/561447/store-pictures-as-files-or-in-the-database-for-a-web-app) – Pondlife Jul 09 '12 at 20:36

2 Answers2

1

Do not save the images to disk, doing so will result in ugly mess sooner or later. What you should do is to allow the user to upload the image, resize/crop the image on the server, convert the image to base64 and save the base64 string into varchar(max) column.

Because you are saving only small images(avatars), this solution should work without any problems. Also when you fetch the image base64 string back from the DB, you can embed it straight into HTML if IE7 is not a requirement. Here is a list of browsers that support Data URI scheme

If embeding base64 into HTML doesn't work out, bulld an ASP.NET page that decodes the base64 string into an image and returns a binary response. Then utilise it in your page like this:

<img src="http://www.example.com/images.aspx?thimageDBrowID=12345" width="20" height="20"/>
Registered User
  • 3,669
  • 11
  • 41
  • 65
  • Being honest, i don't understand your answer! And the images aren't small, are regular pictures, but my code at the moment of saving the images resize it to something smaller (160x120) and save this small version and keeps the big one too – Ivelisse Jul 09 '12 at 20:38
  • 160x120 is a small image. Big image is 4000x2500. Why you save the big one?... Anyway my answer is provided as is. I can't write the code for you remotely. Read the answer few times and if you have a particular question don't hesitate to ask. – Registered User Jul 09 '12 at 20:42
  • 160x120 is the avatar size, i save the big one (size like, idk, digital camera regular size) because can be useful in another sections of the project – Ivelisse Jul 09 '12 at 20:56
  • OK, no problem. Store the avatar in the DB and dump the original to disk for any future use. – Registered User Jul 09 '12 at 20:59
  • that could be a solution but i dont know how to take the resized image from that method that does it and save it into the method that is saving the rest of the fields into the database – Ivelisse Jul 09 '12 at 21:09
  • It will be easy for you to do, StackOverflow is littered with similar examples, just do a search. – Registered User Jul 09 '12 at 21:20
0

If you're using SQL Server 2008 you can look into using FILESTREAM

Levi W
  • 805
  • 6
  • 13