WP7 resumable upload

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.