Streaming binary data [modified]
-
Hi, Is there any way to stream binary data from a SQL database using LINQ? In an ASP.NET application, I want to use a custom HTTP handler to get the binary data in small chunks and send it to the response stream. Otherwise, if I have a 150mb file to download, the web server will have to cache the entire file before the transfer even starts to the client! I am looking for something similar to the SQLDataReader.GetBytes() function, where I can get a few bytes at a time. Any help regarding this is appreciated. Thanks, --Mike
modified on Thursday, March 27, 2008 6:05 PM
-
Hi, Is there any way to stream binary data from a SQL database using LINQ? In an ASP.NET application, I want to use a custom HTTP handler to get the binary data in small chunks and send it to the response stream. Otherwise, if I have a 150mb file to download, the web server will have to cache the entire file before the transfer even starts to the client! I am looking for something similar to the SQLDataReader.GetBytes() function, where I can get a few bytes at a time. Any help regarding this is appreciated. Thanks, --Mike
modified on Thursday, March 27, 2008 6:05 PM
Off the top of my head (and I can't say I've looked into this) but you might be able to use a combination of Skip and Take to achieve this. The data would be mapped to a byte array, so it should (theoretically) be possible. Good luck.
Deja View - the feeling that you've seen this post before.
-
Off the top of my head (and I can't say I've looked into this) but you might be able to use a combination of Skip and Take to achieve this. The data would be mapped to a byte array, so it should (theoretically) be possible. Good luck.
Deja View - the feeling that you've seen this post before.
Hi, Mapping the data to a byte array would require using the Linq.Binary.ToArray() function, which would effectively load the entire array into memory before doing anything with it. Lets say I want the first 5 bytes:
GSTDatabaseDataContext gstDatabase = new GSTDatabaseDataContext(); var fileData = (from f in gstDatabase.Files select f.Data).Single(); byte[] data = (from d in fileData.ToArray() select d).Take(5).ToArray();
[modified - added .ToArray() at the end of the last line] by using fileData's (which is a Linq.Binary type) ToArray() function, it means the ASP.NET server will load the entire array into memory from the SQL Server. My problem is that I want to get the data from the SQL server in small chunks so that I can stream it as it comes. Any suggestions? Why is it so damn difficult to find any information about LINQ online...I searched and searched and apparently nobody has a need to do this :confused: I don't understand. Even this LINQ forum has barely any posts. Is nobody using LINQ for anything?? lolmodified on Friday, March 28, 2008 8:08 AM
-
Hi, Mapping the data to a byte array would require using the Linq.Binary.ToArray() function, which would effectively load the entire array into memory before doing anything with it. Lets say I want the first 5 bytes:
GSTDatabaseDataContext gstDatabase = new GSTDatabaseDataContext(); var fileData = (from f in gstDatabase.Files select f.Data).Single(); byte[] data = (from d in fileData.ToArray() select d).Take(5).ToArray();
[modified - added .ToArray() at the end of the last line] by using fileData's (which is a Linq.Binary type) ToArray() function, it means the ASP.NET server will load the entire array into memory from the SQL Server. My problem is that I want to get the data from the SQL server in small chunks so that I can stream it as it comes. Any suggestions? Why is it so damn difficult to find any information about LINQ online...I searched and searched and apparently nobody has a need to do this :confused: I don't understand. Even this LINQ forum has barely any posts. Is nobody using LINQ for anything?? lolmodified on Friday, March 28, 2008 8:08 AM
For those interested, I got my answer: You can get a DataReader from your LINQ query and do with it what you please. I stumbled upon this, which pointed me in the right direction: http://www.west-wind.com/WebLog/posts/141435.aspx[^] Happy coding! --Mike