views:

406

answers:

5

Hello!

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.

Thank you!

A: 

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> />
ZombieSheep
These images will be accesed throw WCF so I need to store them on SQL Server
VansFannel
and what about audio and video files?
VansFannel
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?
ZombieSheep
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.
ZombieSheep
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.
Zhaph - Ben Duguid
A: 

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.

John Saunders
+3  A: 

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.

Adrian Godong
Lots of other good answers to this question already on SO too:http://stackoverflow.com/questions/386142/ihttphandler-example-required-for-image-type-files-c-asp-net/386171#386171
Zhaph - Ben Duguid
A: 

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.

RedFilter
A: 

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.

Srikanth V M
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
Srikanth V M