views:

564

answers:

1

I have images stored in my database in a FILESTREAM and I am trying to find out what the best solution is to get that image back out into a web browser.

If I was managing the files on the file system myself, the quickest way would just be:

Response.TransmitFile(pathToFile);

This does not load the file into memory before transmitting it back to the client (to my understanding) and as such is nice and speedy.

I'm currently using Linq to SQL to get the FILESTREAM. This provides the FILESTREAM as a Binary object.

So far have this quite ugly way of doing it:

Response.WriteBinary(fileStreamBinary.ToArray());

Am I going to be better off not bothering with the Linq to SQL and doing things more directly?

I'm beginning to wonder why I bothered with FILESTREAM in the first place and didn't just stick to managing the files myself. I'm sure there was a reason for it without using the word "bandwagon"!

+1  A: 

What about that ?

byte[] buffer = new byte[bufferSize];
int nBytes;
while((nBytes = fileStreamBinary.Read(buffer, 0, bufferSize) != 0)
{
    Response.OutputStream.Write(buffer, 0, nBytes);
}

That way you never load the whole stream into memory

Thomas Levesque
Hmmm maybe we are talking about different Binary object types - Linq returns a System.Data.Linq.Binary object which does not have a Read() method :(
joshcomley
Sorry, I misunderstood your code sample and thought that fileStreamBinary was a stream... I'm not familiar with System.Data.Linq.Binary, but it seems to be fetching the whole buffer at once, so you can't avoid loading it in memory. So I'm afraid the code you find "ugly" is the only way...
Thomas Levesque
Looks like I may have to circumvent Linq when using FILESTREAM then. Seems silly seeing as the whole point of FILESTREAM was to allow super-quick direct-from-disk streaming!
joshcomley
You can probably do that by executing a SqlCommand on the DataContext.Connection, and retrieve the data with SqlDataReader.GetBytes
Thomas Levesque