Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Images in DB - Networked Desktop Applications

Related: Storing Images in DB - Yea or Nay?

After reading the above question, it seems the preferred method for image storage with databases is to store only the filepath within the database. However, most of these answers seem to focus on web servers.

In my case, I'm developing a desktop application that will be used across multiple computers within an intranet. A dedicated server will host the database, containing information related to performing tests on various equipment.

Images need to be stored on the server in some way. Would storing the images in the database be the correct approach in this case, or even the only approach?

Pros:

  • Backup is limited to only the database.
  • No need to open up the server's file system to the network.
  • Single protocol for server information access.
  • Protected file access. (User can't go in and delete all the images)

Cons

  • Performance issues in future if there's too many images.

Edit: As stated in the tags, the application is being written in C#/.NET. If writing the images to the file system is an option in this case, I could use some help understanding how this is done.

Edit 2: As elaborated some in the comments below, for now I'm assuming a MySQL database, although the FileStream capabilities of SQL Server 2008 could potentially change that.

Also in my case, images will be added often, and can be considered read-only after this point since they should never be changed, and will just be read out when needed. Images will likely be small (~70k each), and I'm also considering some other binary format storage on the server, files which are ~20k each which I can likely apply the same approach for storing and retrieving.

like image 204
Will Eddins Avatar asked Dec 07 '09 18:12

Will Eddins


People also ask

How do I store images in a database?

Storing Images into a Database. In order to provide your application with cool pictures you can employ two techniques (at least). One of them is that you can save the pictures in a folder and store the path to each one in a database or configuration file. The other one is to store the entire file into a database, along with its file name.

Should web application store images in database or file system?

Should web application store images in Database or File system? We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section. As a web developer storing images in the file system would be the easiest and it is the best way.

What are the disadvantages of storing image data in a database?

There are too many disadvantages to this approach. Storing the image data in the table requires the database server to process and traffic huge amounts of data that could be better spent on processing it is best suited to. A file server can process such image files much better.

What is the best way to store images in an application?

Many applications store images permanently as files. For example, drawing applications store pictures, spreadsheet applications store charts, CAD applications store drawings, and so on. If you are writing an application that stores a bitmap image in a file, you should use the bitmap file format described in Bitmap Storage.


1 Answers

I'd suggest keeping those files on disk in the file system, rather than in the database. File system for files, databases for relational data, etc.

Deliver by Web Service

Consider delivering those images to your desktop app by hosting a web service/app on that DB machine. That app's job it is to serve only images. Setup a web server on that machine with an ASP.NET application. Have an .ashx handle requests and stream the binary image. Something like this:

http://myserver/myapp/GetImage.ashx?CustomerID=123&ImageID=456

Security

If intranet security is an issue, this would be the point where you could ensure that the user is authenticated and authorized for read access to the image. Audit trails could be implemented here as well.

File System Security

Regarding security on those images, consider that NTFS gives you a lot of measures to ensure that only those who are authorized can read/delete/put files as required. The task then would be to define those roles and implement Windows security groups.

Future Needs

This approach allows you to securely consume those images from anywhere on the intranet. Perhaps this app would be migrated to a web application at some point? Perhaps a feature request comes from the customer where a web solution is appropriate?

This might sound like overkill rather than reading a blob from the database, but it's great from a security perspective. Consider your customers' and patients' expectations on privacy and security.

<%@ WebHandler Language="C#" Class="Handler" %>

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) 
{
    //go to the DB and get the path for this ID.
    string filePath = GetImagePath(context.Request.QueryString["ImageID"]);

    //now you have the path on disk; read the file
    byte[] imgBytes=GetBytesFromDisk(filePath);

    // send back as byte[]
    context.Response.BinaryWrite(imgBytes);
}
like image 96
p.campbell Avatar answered Oct 13 '22 01:10

p.campbell