Resumable data transfer is actually a request stream in the middle of the Request stream, instead of coming from the beginning each time.
Httpwebrequest myrq = (httpwebrequest) httpwebrequest. createhttp (URL );
Myrq. headers ["range"] = "bytes = 1024-"; // sets the range value.
This is the stream from the beginning to the end of the request.
If myrq. headers ["range"] = "bytes = 1024-2048 ";
Indicates the data at the 1024 to 2048 locations in the request.
With this method, writing resumable upload is much simpler. The error is omitted as follows:
Public VoidDownfile (StringURL,StringFilename, progressbar prog)
{
// Open the last downloaded file or create a new file
LongLstartpos = 0;
Isolatedstoragefilestream FS;
If(Isolatedstoragefile. getuserstoreforapplication (). fileexists (filename ))
{
FS = isolatedstoragefile. getuserstoreforapplication (). openfile (filename, filemode. Open );
Lstartpos = FS. length;
FS. Seek (lstartpos, system. Io. seekorigin. Current );// Move the current pointer in the file stream
}
Else
{
FS = isolatedstoragefile. getuserstoreforapplication (). createfile (filename );
Lstartpos = 0;
}
Try
{
Httpwebrequest myrq = (httpwebrequest) httpwebrequest. createhttp (URL );
Myrq. headers ["User-Agent"] ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. Net CLR 1.0.3705 )";
If(Lstartpos> 0)
{
Myrq. headers ["Range"] ="Bytes ="+ Lstartpos +"-";// Set the range value
}
Myrq. begingetresponse (NewAsynccallback (a) =>
{
VaR myrp = myrq. endgetresponse ();
LongTotalbytes = myrp. contentlength + FS. length;
This. Dispatcher. begininvoke () => {Prog. Maximum = (Int) Totalbytes ;});
Stream ST = myrp. getresponsestream ();
// Stream so = new filestream (filename, filemode. Create );
LongTotaldownloadedbyte = FS. length;
Byte[] By =New Byte[1024];
IntOsize = ST. Read (by, 0 ,(Int) By. Length );
While(Osize> 0)
{
Totaldownloadedbyte = osize + totaldownloadedbyte;
FS. Write (by, 0, osize );
This. Dispatcher. begininvoke () => {Prog. value = (Int) Totaldownloadedbyte ;});
Osize = ST. Read (by, 0 ,(Int) By. Length );
}
FS. Close ();
St. Close ();
MessageBox. Show ("Download complete! ");
}),Null);
}
Catch(Exception ex)
{
MessageBox. Show ("Prompt"+ Ex. Message );
}
}
Http://blog.bryht.net