0

I want to retrieve an image from SQL Server to show it in an ASP.NET app. I think that I can write down the image retrieved from SQL Server to a server's folder and show it throw its path. Is there any other better way?

I have the same problem with audio and video files (can I use silverlight to play these audio and video files?)

My worry is that I don't want to store these files (images, audios and videos) on server to show it.

5 Answers 5

3

You can write a custom HTTP handler that will take ID of the item you are trying to display on the query string parameter.

This handler will then retrieve the data from SQL Server, and return it just like downloading a file. This post has the steps.

I am not sure about the capability of Silverlight streaming from a "file", you may need to use Silverlight Streaming Service.

1
0

Is there a better way? IMHO, yes there is. Store your image file as a file. That's what the filesystem is for, and you don't get involved in fudging around with the database.

I have, in the past, used MySQL to store images and retrieved them with PHP, but that solution got really old really quickly from a maintenance point of view. It comes down to the fact that you are always going to have to store the data in some way, and the most efficient way is also the simplest.

If you must store it in the database, I'd use a generic accessor page (e.g. myImage.aspx) which queries the database and streams the returned blob directly to the browser. Your img tag would probably then need to look like

<img src="/img/myImage.aspx?imgId=123456" <other tag data> />
5
  • These images will be accesed throw WCF so I need to store them on SQL Server
    – VansFannel
    May 15, 2009 at 9:45
  • and what about audio and video files?
    – VansFannel
    May 15, 2009 at 9:46
  • If you have the bits, regardless of where they come from or what format they are, you can store them whereever you like. Dumping them into the database is a possible solution, sure, but they are just files. Why not use the filesystem? May 15, 2009 at 9:49
  • I should probably add that you will likely still need to store metadata in a database, but I would say store a reference to a filename rather than the content of the file. May 15, 2009 at 9:50
  • You're better off using an .ashx handler rather than a page - it's more lightweight and doesn't have as much "baggage" as a page for this sort of thing. May 15, 2009 at 20:43
0

If you use SQL Server 2008, then I believe you can store these objects as a FILESTREAM, and you can then retrieve the path to the files on the file system. You should be able to arrange for that path to be mapped via IIS, and thus it should be practical to turn it into a URL.

0

Your image source can be

<img src="blah.aspx?id=123" />

In blah.aspx, retrieve the image data from the db and use Response.BinaryWrite to send to the browser, with the appropriate content type set.

0

HI all, I guess the solution for this is present in below links:

http://www.codeproject.com/KB/aspnet/asp_net_and_mysql.aspx

http://www.codeproject.com/KB/aspnet/image_asp.aspx

This solution in for mysql database and also could be suitably modified to work with mssql database as well.

1
  • But i have doubt wrt to dimensions. In the sense that i need to display images in fixed height and width. Can anyone help me on this Oct 16, 2009 at 6:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.