Asked  7 Months ago    Answers:  5   Viewed   28 times

My question is fairly generic and I know there might not be an 100% answer to it. I'm building an ASP .NET web solution that will include a lot of pictures and hopefully a fair amount of traffic. I do really want to achieve performance.

Should I save the pictures in the Database or on the File system? And regardless the answer I'm more interested in why choosing a specific way.

Many thanks, Stefan

DUPLICATE: Storing Images in DB - Yea or Nay?, How to store images in your filesystem, Storing a small number of images: blob or fs? and probably some others.


COMMENT: Thanks for many good answers. I will go for a file based solution even if I like the idea of having a 100% database driven solution. It seems that there are today good solutions to do what I want with databases etc but I have a few reasons for not doing it.

  • I will be on a hosted solution, I have huge amount of storage(10gb) but only 300mb for the database. It will cost a lot for extra storage in the DB.

  • I'm not an DB expert and as well not in control of settings of the DB. A DB based solution might need custom configuration as it looks like.

If we will move to run the site on our own server I might consider a DB based solution. thanks, Stefan

 Answers

98

Store the pictures on the file system and picture locations in the database.

Why? Because...

  1. You will be able to serve the pictures as static files.
  2. No database access or application code will be required to fetch the pictures.
  3. The images could be served from a different server to improve performance.
  4. It will reduce database bottleneck.
  5. The database ultimately stores its data on the file system.
  6. Images can be easily cached when stored on the file system.
Tuesday, June 1, 2021
 
khaverim
answered 7 Months ago
50

This is possible. Tested with iOS5 stand-alone web app:

HTML:

<div id="foz" data-href="http://www.google.fi">Google</div>

JavaScript:

document.getElementById("foz").addEventListener("click", function(evt) {
    var a = document.createElement('a');
    a.setAttribute("href", this.getAttribute("data-href"));
    a.setAttribute("target", "_blank");

    var dispatch = document.createEvent("HTMLEvents");
    dispatch.initEvent("click", true, true);
    a.dispatchEvent(dispatch);
}, false);

Can be tested here: http://www.hakoniemi.net/labs/linkkitesti.html

Wednesday, June 2, 2021
 
Zeth
answered 6 Months ago
46

There won't be much difference in performance between the two methods. Obviously using an http handler will be the fastest you could get because the request doesn't go through the MVC lifecycle (routing, instantiating a controller, model binding, invoking the action) but I think this is a micro optimization and I would personally use the first approach as it is more adapted in an MVC scenario. If you later realize that this is a bottleneck for your application by performing extensive load tests you could always switch to the http handler approach.

As far as your second question is concerned about the helper, the answer is no, you cannot do this easily. The only possibility is to use the data URI scheme but this is not supported by all browsers. This way if your model has the byte array of the image you could write a helper which renders the following:

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAA..." alt="Red dot" />

The image data is base64 encoded directly into the page. Another drawback is that those images will never be cached and your HTML pages could become very large.

Tuesday, August 10, 2021
 
tpow
answered 4 Months ago
80

Actually your datastore look up with the database may actually be faster depending on the number of images you have, unless you are using highly optimized filesystem engine. Databases are designed for fast lookups and use a LOT more interesting techniques than a file system does.

reiserfs (obsolete) really awesome for lookups, zfs, xfs and NTFS all have fantastic hashing algorithms, linux ext4 looks promising too.

The hit on the system is not going to be any different in terms of block reads. The question is what is faster a query lookup that returns the filename (may be a hash?) which in turn is accessed using a separate open, filesend close? or just dumping the blob out?

There are several things to consider, including network hit, processing hit, distributability etc. If you store stuff in the database, then you can move it. Then again, if you store images on a content delivery service that may be WAY faster since you are not doing any network hits on yrouself.

Think about it, and remember bit of benchmarking never hurt nobody :-) so test it out with your typical dataset size and take into account things like simultaneous queries etc.

Saturday, September 25, 2021
 
Abdel
answered 2 Months ago
83

Depending on how you are getting your images a variation of this might work

<html>
    <body>
        <img src="<dynamic handler url>" alt="My Username" onError="this.src='defaultProfile.jpg';" />
    </body>
</html>

This is how you would do it in ASP.NET.

Designer -

<asp:Image ImageUrl="NonexistentImage.Jpg" ID="profileImage" Height="50" Width="50" runat=server />

Code Behind (c#)

profileImage.Attributes["onerror"] = "this.src='http://www.cs.uofs.edu/~olivetoj2/blah.jpg';";

This works perfectly for me.

Wednesday, November 3, 2021
 
j3d
answered 4 Weeks ago
j3d
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share