7

I am using asp.net mvc and have a section where a user can upload images. I am wondering where should I store them.

I was following this tutorial and he seems to store it in the app_data. I however read another person say it should only hold your database.

So not sure what the advantages are for using app_data. I am on a shared hosting so I don't know if that makes a difference.

Edit

I am planning to store the path to the images in the database. I will be then using them in a image tag and rendering them to the user when they come to my site. I have a file uploader that only will expect images(check will be client and server)

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Store image content in file system and image path in database. Why? Read [this post](http://stackoverflow.com/questions/561447/store-pictures-as-files-or-in-the-database-for-a-web-app) – Emmanuel N Dec 26 '11 at 20:06
  • Small remark: if you ever in the future need to scale out horizontally, storing uploaded content in a folder on your web server will become a problem. If this is a concern to you, consider using services like azure blob storage, amazon simple storage or even a database as suggested by some of the answers below. – santiagoIT Dec 26 '11 at 21:39

3 Answers3

6

The tutorial is a simple example - and if you read the comments, the original code just saved to an uploads directory, no app_data in sight.

It was changed to app_data because that's a special folder - one that will not allow execution of code.

And you have understood correctly - app_data is really there for holding file based databases. That's the meaning of the folder. As such saving images into it doesn't feel like the right thing to do.

If you are certain only images will get uploaded (and you control that), I would suggest an /uploads directory - a reserved location for images that also will not allow code execution (something that you should be able to control via IIS).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Is the uploads a special folder or something you just made? – chobo2 Dec 26 '11 at 20:16
  • @chobo2 - Just something I mentioned as it was done originally in the article you linked to. – Oded Dec 26 '11 at 20:17
  • I don't know how much control I have on IIS as I said I am using shared hosting. I will be storing the relative path in the database then getting the full path that will be used in a img tag. So I am not sure if that counts as code execution. – chobo2 Dec 26 '11 at 20:39
1

I would say that depends on what you will do with that images later. If you use those images in an img tag, you could save them somewhere in the Content/ folder structure.

If you do not need them reachable from the outside, or need to stream them changed back, you might store them out of the web root if the hoster allows for that.

I wouldn't store them in app_data, as I - personally - think that it's more a convention to store there a database. Most developers not familiar with that product wouldn't look there for the image.

But: you could store binaries in a db (even though it is probably not the best thing to do), so a reference in a db pointing to a file in the same directory makes sense again.

It's more an opinion thing than a technical question though, I think.

Sascha
  • 10,231
  • 4
  • 41
  • 65
0

I prefer to store them in the database. When storing images on the file system I've found it can be a bit harder to manage them. With a database you can quickly rename files, delete them, copy them, etc. You can do the same when they're on the file system, but it takes some scripting knowledge.

Plus I prefer not to manage paths and file locations, which is another vote for the database. Those path values always make their way into the web.config and it can become more difficult to deploy and manage.

ryan1234
  • 7,237
  • 6
  • 25
  • 36
  • 1
    The only time you would ideally store in a DB is when you use a SQL file type which maps to the file system. The performance implications are well known when you dont. – Adam Tuliper Dec 26 '11 at 22:21