views:

317

answers:

5

I have a project where a user for the company will use a winform HTML editor to put some text and pictures in for specific customers. The customers will be looking at this information on a website.

What's the best way to handle this? If it was strictly HTML text then I could save everything in the database and not have any problems displaying it. But the employee wants the ability to show pictures...


  1. Do I save the pictures as a file in some public place so that the website and the winform program can see it? The problem here would be where is the best public place to store the pictures. The webserver will be a locked down server with no one having access. Can I get the file from a non-local directory from ASP.Net?

  2. Do I save the pictures in the database and make the program smart enough to replace the image source tags with the picture from the database? I thought this might get difficult (definitely on the web... if someone suggests this, can you give me some C# examples on how to do this on the winform and web?)

I know someone else had to do this somewhere and succeeded...

A: 
  1. Yes, if you don't plan to limit the access to these images. If you do, store them away from www root and send to browser by script.

  2. No. Better to store images on disc and relative path to them - in DB. Database is not a correct place to store files.

FractalizeR
A: 

Please see this

http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay#3756

and other older questions about this subject. In my opinion, either choice is possible and legitimate, and so is a combination of both (storing file references in the database). You should make your decision based on specific circumstances in your project (that you did not provide), such as

  • number of images/files expected
  • capabilities and performance considerations of the specific database that you would use, in regards to storing blobs
cdonner
A: 

You can easily store the images in the database and then retrieve them. To display images from a database you can either use a asp.net page (see this article) or an HttpHandler.

You can also store the files in a local folder. Regular read/write file operations can be used. In order for this to work the ASP.NET user ([Local Machine]\ASPNET or NETWORK SERVICE) needs read/write access to that folder. To use a remote folder things are more tricky and you may create a security hole while trying to do so.

As a conclusion, I believe that the best solution would be to use a local folder. If this impossible, avoid exotic solutions (FTP, user impersonation, web service to upload/load images) and use the database.

kgiannakakis
+4  A: 

I would suggest using a database to store a path to a local image (if this is possible on your setup)

EG the physical image will be saved in a path like the following:

/App_Themes/Web/images/userfiles/YOURIMAGENAME.jpg

and just save the following in the database

YOURIMAGENAME.jpg

This would then mean that you could have somthing like the following:

<asp:Image ID="imgImage" runat="sever" />

protected void Page_Load(object sender, EventArgs e)
{
    //Your data-access code here

    //image.ImageName is provided by your data access code.
    imgImage.ImageUrl = string.Format("/App_Themes/Web/images/userfiles/{0}", image.ImageName)
}
GaryDevenay
A: 

If using the file system is not possible, consider storing your images in cloud storage such as Amazon S3.

You can configure S3 objects so that they're public read but private write, so that only your software can add images to S3 but anyone on the web can view them. Depending on your security requirements, you can either encode the S3 secret keys in your WinForms app to construct PUT URL's or create a simple web service on the app server that authenticates the user and uses the secret keys on the server to create PUT URL's. The PUT URL's can then be returned to the client, which can use to upload the image data.

Using S3, you'd then upload the images using HTTP PUT and both the editor and web browser would access the images as usual using HTTP GET. There are C# libraries available and plenty of sample code on the web for access to S3.

You could actually do the same with your HTML data as well.

Note that there are costs associated with S3 (see pricing) but assuming the costs are within bounds it's a great solution to this type of problem.

dmercredi