PassionateDeveloper
PassionateDeveloper

Reputation: 15138

How to save uploaded pics in database?

in my new c#.net 3.5 ASP Website, user have the possibility to upload a picture.

What solution is the best for saving this picture? Should I save it on my server in a folder and save the path in the database, or should I save the picture in the database?

Upvotes: 2

Views: 678

Answers (3)

Jon Hanna
Jon Hanna

Reputation: 113272

Pro files:

  1. There is a simple call to make to serve a file to the response stream. Just set the ContentType appropriately, call that, and you're done.
  2. Its more performant (though this is often overstated, good use of blobs or SQLServer filestreams tends to be pretty performant, but still lose compared to file operations).

Pro DB:

  1. Consistency is easier, you can't have the situation where the file structure and database mismatch.
  2. File-based solutions are tricky if you move to a web-farm solution with more than one webserver (above a certain amount of success you will have to do this).
  3. Migration of data due to modifications is generally easier. You're going to have to move the DB data somewhere anyway, so once you've solved that you've solved it for images too.
  4. Manipulation code is easier to write.
  5. Reporting on the whole collection of images is easier.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160902

It is not recommended to store large files/pictures in the DB, but you can. For that use a column of type varbinary.

As of SQL Server 2008, you can also save BLOBs=files on the disk but still have SQL server refer to them with the help of the filestream storage attribute. You can check basics of the performance differences here.

Upvotes: 0

user151323
user151323

Reputation:

Should I save it on my server in a folder and save the path in the database

You could do something like that or...

you can take advantage of the FILESTREAM feature in SQL Server 2008.

Files will be stored on disk but transparently manipulated as though they were directly in a database field. Lessens load on the database and constraints the database explosive growth, especially if you take advantage of NTFS streaming for accessing those files.

An Introduction to SQL Server FileStream

Upvotes: 0

Related Questions