IOS implements simple resumable download nsurlconnection

Source: Internet
Author: User

Reproduced from: http://blog.csdn.net/sirchenhua/article/details/7286312? Reload

The key to implementing resumable data transfer through nsurlconnection is to customize the range field attribute of the HTTP request header.

Range Header
The range header field can request one or more sub-ranges of an object. For example,
Indicates the first 500 Bytes: bytes = 0-499
Indicates the second 500 Bytes: bytes = 500-999
Indicates the last 500 Bytes: bytes =-500
Indicates the range after 500 Bytes: bytes = 500-
First and last Bytes: bytes = 0-0,-1
Specify the following ranges: bytes = 500-600,601-999.
However, the server can ignore this request header. If the unconditional get contains the range request header, the response will be returned with the status code 206 (partialcontent) instead of 200 (OK ).

Use nsmutableurlrequest in IOS to define the header domain

  1. Nsurl * url1 = [nsurl urlwithstring :@"";
  2. Nsmutableurlrequest * request1 = [nsmutableurlrequest requestwithurl: url1];
  3. [Request1 setvalue :@"Bytes = 20000 -"Forhttpheaderfield :@"Range"];
  4. [Request1 setcachepolicy: nsurlrequestreloadignoringlocalcachedata];
  5. Nsdata * returndata1 = [nsurlconnection sendsynchronousrequest: request1 returningresponse: Nil error: Nil];
  6. [Self writetofile: returndata1 filename :@"Somepath"];
  7. -(Void) Writetofile :( nsdata *) Data filename :( nsstring *) filename
  8. {
  9. Nsstring * filepath = [nsstring stringwithformat :@"% @", Filename];
  10. If([[Nsfilemanager defaultmanager] fileexistsatpath: filepath] = No ){
  11. Nslog (@"File not exist, create it ...");
  12. [[Nsfilemanager defaultmanager] createfileatpath: filepath contents: Nil attributes: Nil];
  13. }Else{
  14. Nslog (@"File exist !!! ");
  15. }
  16. File* File = fopen ([filename utf8string], [@"AB +"Utf8string]);
  17. If(File! = NULL ){
  18. Fseek (file, 0, seek_end );
  19. }
  20. IntReadsize = [Data Length];
  21. Fwrite ((Const Void*) [Data bytes], readsize, 1, file );
  22. Fclose (File );
  23. }

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.