Tuesday, March 13, 2012

Should I save images to a database or to a file system?

I am using VB, visual web development express and the sql express that come with it. While building a user interface for saving pictures by users, I would like to know what will be esier to implement, more fuctional from the host perspective and what is the method that is most used in this case and why? In other word should I go with database images saving or file system images saving?

Thanks

It's easiest to store the files on disk. Also, most hosting providers give you more diskspace when compared to your maximum database size. Lastly, I think retrieving files from disk is usually faster.


IMHO,

The previous poster is correct, it's *easier* to save images to disk. I prefer saving them to a DB for the following reasons:

1. No special permissions need to exist on the web server to allow files to be saved.
2. You don't need to worry about overwriting files with the same filename, nor do you need to check for dup names.
3. Database sizes are generally larger but they usually don't count against your file quota.
4. The files are as portable as the database itself. If the application ever moves, the images remain with the DB.
5. Images are not relative to the root of the application, so they become useable by other applications from one source, if necessary.
6. There are *LOTS* of good examples in both VB and C# detailing the steps required to do it.

With all that said, there are a few drawbacks:

1. Database size can get out of hand if you save images of any size and any resolution. I generally resize the image to a fixed size and generate a thumbnail before saving it to the DB.
2. I've had trouble occasionally saving some (but not all) animated gifs for some reason, really never figured that out.
3. This method will require that you write an aspx page to display the image, but it's not complicated codebehind.
4. This method will require you to use stored procs to insert the image, not a big deal but more complicated than a simple insert statement.

Hope that helps.


If youstore images in the database you need to process the images data 2 times.

1) Whenthe image is uploading
2) Whenthe image is displaying

But ifyou store it in file system you need to process the image only one time. Thatis when the image is uploading.


I would agree with the previous posters - storing photos in the file system is far easier. Here is an example of where I use a detailview to display staff information, including a photo:

 <asp:DetailsView ID="dgDetails" runat="server" AutoGenerateRows="False" DataSourceID="SqlStaffDetails" CellPadding="4" HeaderText="Details"> <Fields> <asp:BoundField DataField="lname" HeaderText="Last Name" SortExpression="lname" /> <asp:BoundField DataField="fname" HeaderText="First Name" SortExpression="fname" /> <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" /> <asp:BoundField DataField="phone" HeaderText="Office Phone" SortExpression="phone" /> <asp:BoundField DataField="cellphone" HeaderText="Cell Phone" SortExpression="cellphone" /> <asp:BoundField DataField="location" HeaderText="Office Location" SortExpression="location" /> <asp:BoundField DataField="floor" HeaderText="Floor" SortExpression="floor" /> <asp:BoundField DataField="room" HeaderText="Room Number" SortExpression="room" /> <asp:BoundField DataField="division_name" HeaderText="Division" SortExpression="division_name" /> <asp:BoundField DataField="division_phone" HeaderText="Division Phone" SortExpression="division_phone" /> <asp:BoundField DataField="division_fax" HeaderText="Division Fax" SortExpression="division_fax" /> <asp:BoundField DataField="staff_email" HeaderText="Email Address" /> <asp:ImageField AlternateText="No Photo Available" DataImageUrlField="sname" DataImageUrlFormatString="~\StaffPhotos\{0}.jpg" HeaderText="Photo" ReadOnly="True" /> </Fields> </asp:DetailsView>

The asp:ImageField line is where the magic happens. Now, I use a naming convention that each person's photo uses the first part of their email address as the file name - so as my email address is kray@.mississippi.org, the name of the photo is kray@.jpg. The SQL Data Source contains the following code to get "sname":

 LOWER(LEFT(LTRIM(email),PATINDEX('%@.%', email) - 1)) AS sname
Now, the only thing that you will have to do is work out how to upload the photos to the file system. You will need to code the photo upload page to make sure the photos go into the correct directory. There are several examples of file upload around, the only catch might be getting the security setting correct on the upload directory to allow the web application to update that directory.

After reading the previous posts, I would agree, that both the techniques has their own advantages and drawbacks. Befor jumping to any conclusions, I would suggest you think with your system and requirements in mind.

You can consider following points to help you decide:

Usually if you are going to have a large number of images, it is always better to go for database, as to make them portable and accessible from multiple projects.

I read all your answers and they are very clear and reasonable. Since to be that one is not better than the other because there are pros and cons for each. Now, a company like EBAY, that has to handle millions and millions of images (you may not know the answer, but can you guest), which one do they use, file saving or database? Which one is more functional and better in their case?

Thanks


They problably are saving the image file path in the database, not the actually image.

0 comments:

Post a Comment