0

I'm trying to create a sample ASP.NET MVC application with a ViewModel and onion architecture - very simple online shop.

So as you suppose this shop has products, and each product should have one very small image and when user clicks on that product, he is redirected to a details page, and of course he should see a bigger image of the product.

AT first I thought, it's a simple application, I would (internet) links to the pictures in the database. But then I thought, ok what about when this image is erased from internet, my product will no longer have an image.

So I should store those pictures in the database somehow. I have heard about something called FileStream that is the right way but I found no material to understand what is that.

I hope someone would help me.

3
  • 2
    One thing you can do is, saving images into a particular folder in your hosting server (or where you want to) and then saving that image name and location into database for corresponding product. Commented Feb 4, 2014 at 7:42
  • How are you doing database access? There is a SqlFileStream class, but there are issues with using that directly. I once found code that streamed files directly from the database to the HttpContext.Current.Response.OutputStream. The result was that a database connection was held open as long as the client was streaming the file, which in this case was a 40mb video file. Thus the average life time of this connection could be measured in minutes. Commented Feb 4, 2014 at 8:03
  • Or, you could rename the images to the product database IDs, then you won't even have to save paths or image names. Commented Feb 4, 2014 at 8:03

1 Answer 1

1

There are several options. You could save the picture in the database using a varbinary.

Read here how to read it using MVC.

When you opt for a solution where you split database and file storage, which is perfectly possible, you should consider that it could mean extra maintenance for cross-checking deleted records, etc.

If you choose the last option, the information in the article will mostly suite your needs too.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I know taht I can save them in the database - but I Heardfrom a technical manager that - saving in database would make it so big that nobody makes it. So i'm asking - is it better to save them somewhere in a folder in the server - (in my case localhost) and maybe save in the database - the path and name of the picture. Is it the better approach.
Depends. How large are the images? Is there a need for them in the database? I am not sure how it works in SQL Server, but in Oracle the binaries are (secretly) separated from the actual table, so it means no performance loss.
My main advise against storing in the database as a varbinary is that when you load it into .net you will need to allocate an appropriate byte[]. Given images are likely to be larger than ~70kB in size, you will find perf issues due to memory fragmentation (which is more serious than it sounds).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.