how to bind image that is saved in filestream database sql server 2008 to a image control
i have used filestream of sql server 2008 fo开发者_Go百科r storing images. i want to retrive that image and bind to image control.but not getting the correct path. please help.
You could to write a page which:
- Receive that image id as a query string parameter
- Get that image binary from your database
- Correctly set your
Response.ContentType
- Write those binary data using
Response.BinaryWrite
or similar
You can get information for some keypoints by reading this article: Creating Thumbnail Images on the fly with ASP.Net
EDIT: Here some examples specifically to FILESTREAM:
- Show images on Grid View from File Stream SQL Server 2008
- SQL Server 2008 Filestream and vb.net
I believe the filestream column type is very similar to a binary column with the exception that SQL Server stores it on the filesystem rather than in the actual SQL database files. In any case, you'll need to use some sort of intermediary component to read the binary data and output the contents as a response.
One way to do this is by using a HTTPHandler to which you'd pass some data you could use to identify the record to pull:
public void ProcessRequest (HttpContext context) {
// Add some sort of validation if need be
var id = context.Request.QueryString["ImageID"]))
// Pull your data here based on that information
var dataObject = GetDataObject(id);
context.Response.ContentType = "image/png"; // or the applicable mime content type
context.Response.BinaryWrite((byte[])dataObject[0]["ImageFile"]);
}
It's a brief example and you'd want to make sure you had your logic to pull either a DataTable or a DataReader or something. This example assumes you'd have a DataTable. With a reader you'd need to add in the logic to read the rows.
You'd end up calling the image like this:
<img src="/imagehandler.ashx?ImageID=345" alt="" />
Josh
精彩评论