Ios network learning ------ 11 native API File Upload breakpoint resume

Source: Internet
Author: User


# Import "MainViewController. h "@ interface MainViewController () @ end @ implementation MainViewController-(void) viewDidLoad {[super viewDidLoad]; // download the object [self download];}-(void) download {// 1. NSURL * url = [NSURL URLWithString: @ "http: // loacal /~ Apple/itcase/download/itunesconnect_{guide_cn.zip "]; // 2 NSRequest // to determine the file size on the network server, use the Http HEAD method NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // use the HEAD method to obtain only the information of the target file, without actual download. // [Request setHTTPMethod: @ "HEAD"];/** set the idea of resumable data transfer: HeaderField: header field (the field in the request header) you can gradually download data within the specified range by specifying the range. After the download is complete, splice the data into a file. 1. Obtain the total size of the file to be downloaded Based on the HEAD method. 2. Create a temporary buffer file on the disk, the file size is the same as the target file size. 3. All the bytes in the buffer file are 0 by default. 4. multithreading is enabled, and data blocks specified by different range headers are loaded separately, after the data block is loaded, it is written to the corresponding offset address. 5. After all the data is downloaded, the file is downloaded and the temporary file name is changed to the target file. Difficulty in development: 0 before writing a file, you must first create a file of the same size. 1. The file read/write problem is overwritten by default in the oc. If you want to specify the position, you need to use the seek method to move the file pointer. 2. File Locking is a problem when multiple threads write files. */[Request setValue: @ "bytes = 0-499" forKeyPath: @ "range"]; // It indicates that only 0th bytes of data are read to 499th bytes. // 3 NSURLConnection // if you want to get the file length, you can get NSURLResponse * Response = nil; NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; // In the expectedcontentlength attribute of response, you can know the object length of the file to be detached. 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.