File Chunking

  • Thread starter Thread starter Jonathan Eggert via .NET 247
  • Start date Start date
J

Jonathan Eggert via .NET 247

Anyone know any component or samples on how to chunk a big file(approx 5MB
+)
on the sending and receiving side ? File could be an image orplain text. Is it as simple as just looping through the streamon the sending side, sending a byte array and appending to afile on the server side, or does it require a much morecomplicted algorithm?
 
no, you are right, unless compression is involved just chunk it
out.................

byte[] buffer = socket.GetSocketOption(SocketOptionLevel.Socket,
SocketOpionName.SendBuffer);
int bytes = 0;

using (FileStream fromStream = new FileStream("path", FileMode.Open,
FileAccess.Read, FileShare.Read, buffer.Length))
{
while(bytes = fromStream.Read(buffer, 0, buffer.Length) > 0)
{
socket.Write(buffer, 0, bytes);
}
}


Anyone know any component or samples on how to chunk a big file (approx 5MB
+)
on the sending and receiving side ? File could be an image or plain text. Is
it as simple as just looping through the stream on the sending side, sending
a byte array and appending to a file on the server side, or does it require
a much more complicted algorithm?
 
Jonathan said:
Anyone know any component or samples on how to chunk a big file
(approx 5MB+)

' send picture in 32KB blocks
const BLOCKSIZE as Integer=32768
' adjust next line to as required
Dim filepath as String="D:\pictures\" & Request.QueryString("f")
Dim objFileStream as FileStream=new FileStream(filepath, FileMode.Open,
FileAccess.Read)
Dim fileSize as Integer=objFileStream.Length
Dim offset as Integer=0
Dim length as Integer=0
Dim Buffer(BLOCKSIZE) as Byte
' change the filename= part as appropriate or leave it out completely
Response.AddHeader("Content-Disposition", "attachment;filename=" &
Request.QueryString("f"))
Response.AddHeader("Content-Length", fileSize.ToString())
Response.ContentType = "application/force-download"
while (offset<fileSize)
length = fileSize-offset
if (length<BLOCKSIZE) then
redim Buffer(length)
end if

if (length > BLOCKSIZE) then
length = BLOCKSIZE
end if
objFileStream.Read(Buffer, 0, length)
Response.BinaryWrite(Buffer)
Response.Flush()
offset+=length
end while
objFileStream.Close()

(some lines may wrap)

Andrew
 
Back
Top