Here's my implementation, where the server portion sits behind a firewall communicating through a remoting service to a HTTP Handler, which renders a PDF document. This same technique could be used for any type of file especially considering constraints with File Uploader controls.
Server - ChunkBytes function takes three parameters: document ID (to look up path), Maximum chunk size, and offset position.
ChunkBytes(docID as integer, maxChunk as integer, offset as integer) as Byte()'first convert file to document streamDim documentStream as FileStream = New FileStream(fileLocation, FileMode.Open, FileAccess.Read)Dim newArray as Byte()'if the offset position is greater than the length return NothingIf offset > documentStream.length - 1 thennewArray = NothingElse'create the chunk as a byte arrayDim byteCount as integerIf maxChunk > documentStream.length - offset'if we're on the last chunk, the array size will be smallerbyteCount = documentStream.length - offsetElse'otherwise set to maxChunkbyteCount = maxChunkEnd If'create the new array with the correct sizenewArray = New Byte(byteCount - 1) {}'position the file stream to the correct positiondocumentStream.Seek(offset, SeekOrigin.Begin)'read the chunk from the filestream into the new arraydocumentStream.Read(newArray, 0, byteCount)End IfReturn newArrayEnd Function
The client in our case is on the HTTP Handler inside the ProcessRequest sub. Each chunk is written directly to the http context, however in another setting you could collect the chunks in another array and output as needed.
'create the maximum chunk size the network can handle we're using 10 MbDim maxChunk as Integer = 10 * 1024 * 1024Dim offset as Integer = 0'get the length of the documentDim documentLength as Integer'now get the first chunkDim documentBytes as Byte() = DocumentByte.ChunkBytes(ID, maxChunk, offset)'keep getting the next chunk until the function returns nothingDo Until documentBytes Is Nothing'write the result to the contextResponse.BinaryWrite(documentBytes)'incriment the offsetoffset += maxChunk'get the next chunkdocumentBytes = DocumentByte.ChunkBytes(ID, maxChunk, offset)Loop