iOS elearning------11 Native API File upload breakpoint continuation idea

Source: Internet
Author: User


#import "MainViewController.h" @interface Mainviewcontroller () @end @implementation mainviewcontroller-(void)    viewdidload{[Super Viewdidload]; Download file [self download];} -(void) download{//1. Nsurl Nsurl *url = [Nsurl urlwithstring:@ "Http://loacal/~apple/itcase/download/iTunesConnect_DeveloperGuide_CN.zip"    ]; 2NSRequest//To determine the size of a file on a network server, you can use the Head method of http Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url            ];    Using the head method, only the information of the target file is obtained, not the actual download work.    [Request sethttpmethod:@ "HEAD"];          /** Set Breakpoints: Headerfield: Header Domain (field of the request header) you can incrementally download the data within the specified range by specifying a range, and then stitching the data into one file after the download is complete. 1 According to the head method to get to the total size of the file to be downloaded, 2 to create a temporary buffer file on the disk, the size of the file and the target file size consistent 3 buffer file all the bytes are default to 0 4 open multi-threaded, respectively load different range header specified data block, the data block loading is completed to     , it is written to the corresponding offset address respectively.          5 After all the data has been downloaded, the file download is complete, and the temporary filename is changed to the destination file.     Development difficulties: 0 before writing a file, you must first create a file of the same size.     1 file read and write problems, in OC default is overwrite, append, if you want to specify the location, need to use the Seek method, move the file pointer.     2 Locking the file is an issue when writing to a file in multiple threads. */[Request setvalue:@ "bytes=0-499" ForkeThe ypath:@ "range"];//indicates that the data is read only from the No. 0 byte to the No. 499 byte.    3NSURLConnection//If you want to get the file length, you can get nsurlresponse *response = nil in response;    NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil];    In the Expectedcontentlength property of response, you can learn the file length of the file you want to unload.       NSLog (@ "%lld%d%@", [response expectedcontentlength], data.length, data); } @end












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.