1

Possible Duplicate:
Storing Images in DB - Yea or Nay?

I am making an asp.net mvc site that will have a forum to upload some information. I want to be able to have it upload images. However I am not sure if I should save these images in the database or save them to the file server.

I do not see the images going over 200Kb (this is probably very high). I think the average will be under 50kb.

So what is better to use?

Cœur
  • 37,241
  • 25
  • 195
  • 267
chobo2
  • 83,322
  • 195
  • 530
  • 832

5 Answers5

5

If you don't need to control access to those images ( i.e. they are public, not role-based ) then store them on the server and keep a link to the path in your database.

Problem with going for the DB solution is that you have to stream them out. Could be a performance drag on a forum page with a lot of images.

And as I always say, why code for serving files when a webserver can do it so much better?

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
  • 1
    +1 And you get the benefit of (assuming you'll be showing these images as well uploading them) images being cached browser side (and you'll find getting images from a model/viewmodel to the browser will incur a lot of database action). – K. Bob Dec 15 '11 at 17:24
3

Personally I would opt to store the images on the server and then have a path to each saved in a table. Performance wise it can be much better.

CSharpened
  • 11,674
  • 14
  • 52
  • 86
3

Depends on what you value and what you're doing with them:

Images in a database:

  • Live with the associated data, no chance of becoming detached when data is migrated to another system
  • Easier to back up (because they're all with the database)
  • More secure, harder to manipulate either accidentally or deliberately

Images on the filesystem

  • Save space on the DB server storage - may be important if you're in a shared environment paying for DB space
  • easier to retrieve in code
  • easier to reference in html

I don't think there's a "best" approach for all situations - but having said that, if it's for a web site then filesystem would seem to make more sense to me!

MarcE
  • 3,586
  • 1
  • 23
  • 27
1

I developed a real estate application some years ago, and opted to store the images for each property in the database. The performance was horrific. I then changed it to store images in the file system (which was a pretty long operation to perform) and the performance was much faster.

I would say the only items you should store in the database are large text documents so they could be used for something like SQL Server's full text search. However, I'd never store images in the database, especially if there are potentially multiple images per page.

Store the URL in the database, then fetch the image from the file system.

Paul
  • 3,072
  • 6
  • 37
  • 58
  • I developped a CMS about 5 syears ago and we found the perforamnce terrifivc in a positive sense. So - sure it is not you not knowing how to efficiently deal with binary data in a database? – TomTom Dec 15 '11 at 17:23
1

I have an example of efficient, streaming based, solution for storing and retrieving media from a SQL Server database: Download and Upload Images from SQL Server via ASP.NET MVC and a follow up FILESTREAM MVC: Download and Upload images from SQL Server.

I would also recommend you to read To BLOB or not to BLOB.

My advice though would be that, unless you have specific reasons to do otherwise, to store the media in the file system. HHTTP servers are extremely efficient at simply streaming back a file. The specific reasons to use a DB instead would be things like access control, self contained backup-restore, or a high availability/disaster-recoverability solution centered around the DB.

And remember that the fastest response is when the browser never asks :) Use caching tags on your responses.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569