iOS file segment Download

Source: Internet
Author: User

HTTP HEAD Generation method

Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0 timeoutInterval:kTimeout];

Request. HttpMethod = @ "HEAD";

[Nsurlconnection sendasynchronousrequest:request queue:self.myQueue completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

NSLog (@ "%@", response);

NSLog (@ "---------------");

NSLog (@ "%@", data);

}];

Run the test code to discover that the Head method simply returns the resource information without returning the data body

Application Scenarios:

(1) Get Resources mimetype (2) Gets the resource file size for endpoint continuation or multi-threaded downloadways to use code blocks to get the size of a resource

-(void) Filesizewithurl: (nsurl *) URL completion: (void (^) (long long contentlength)) completion

{

Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0 timeoutInterval:kTimeout];

Request. HttpMethod = @ "HEAD";

Nsurlresponse *response = nil;

[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];

Completion (RESPONSE.EXPECTEDCONTENTLENGTH);

}

determine the pseudo-code implementation of each download packet

-(void) Downloadfilewithurl: (nsurl *) URL

{

[Self Filesizewithurl:url completion:^ (long long ContentLength) {

NSLog (@ "Total file size:%lld", contentlength);

Download files by size

while (ContentLength > Kdownloadbytes) {

NSLog (@ "Each download length:%lld", (Long Long) kdownloadbytes);

ContentLength-= kdownloadbytes;

}

NSLog (@ "Last downloaded bytes:%lld", contentlength);

}];

}

Segmented Range Code Implementation

Long long frombytes = 0;

Long long tobytes = 0;

while (ContentLength > Kdownloadbytes) {

Tobytes = frombytes + kDownloadBytes-1;

NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", Frombytes, tobytes];

NSLog (@ "range%@", range);

Frombytes + = Kdownloadbytes;

ContentLength-= kdownloadbytes;

}

Frombytes = frombytes + contentLength-1;

NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", Frombytes, tobytes];

NSLog (@ "range%@", range);

Segment Download File

Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy: Nsurlrequestreloadignoringcachedata Timeoutinterval:ktimeout];

NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", from, end];

[Request Setvalue:range forhttpheaderfield:@ "range"];

Nsurlresponse *response = nil;

NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];

NSLog (@ "%@-%@-%ld", Range, response, (unsigned long) data.length);

Tips:

If get contains a range request header, the response is returned as a status code of 206 (partialcontent) instead of a (OK)

Writing data to a file

Open cache file

Nsfilehandle *FP = [Nsfilehandle FileHandleForWritingAtPath:self.cachePath];

If the file does not exist, write the data directly

if (!FP) {

[Data WriteToFile:self.cachePath Atomically:yes];

} else {

Move to end of file

[FP Seektoendoffile];

Append a data file to the end of a file

[FP Writedata:data];

Close file handle

[FP CloseFile];

}

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.