"iOS Dev-98" File download: @ "HEAD" and [Request Setvalue:range forhttpheaderfield:@ "range"]

Source: Internet
Author: User

(1) file download, the general download can be downloaded directly. Download for video or download software needs to be downloaded in sections. The following is a staged download of the simulation, that is, using the request setting range, this range is a "bytes=1-1024" or the scope of the request or download.


(2) write with Nsfilehandle control file.


(3) Request header file head only, can obtain information without downloading the file.


#import "ViewController.h" #define Ksizepertime 50000@interface Viewcontroller () @property (weak, nonatomic) Iboutlet    Uiimageview *imgview; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "test.jpg" withextension:nil];    1. Get file size long long fizesize=[self filesizewithurl:url];    Compare local whether there are files long long localfile=[self localfilesize];        if (localfile==fizesize) {NSLog (@ "file already exists");    Return    }//2, determine packet size long long fromb=0;    Long Long tob=0;        while (fizesize>ksizepertime) {tob=fromb+ksizepertime-1;        3. Download the packet [self downloadwithurl:url fromb:fromb Tob:tob];        Fizesize-=ksizepertime;    Fromb+=ksizepertime;    } tob=fromb+fizesize-1;    [Self Downloadwithurl:url fromb:fromb Tob:tob]; NSString *path=[[nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject]    stringbyappendingpathcomponent:@ "1.jpg"]; Self.imGview.image=[uiimage Imagewithcontentsoffile:path];} -(Long Long) Filesizewithurl: (Nsurl *) url{nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url    Cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:2.0f];    [email protected] "HEAD";    Nsurlresponse *response=nil;    [Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL]; return response.expectedcontentlength;} -(Long Long) localfilesize{nsstring *path=[[nssearchpathfordirectoriesindomains (nscachesdirectory,    Nsuserdomainmask, YES) lastobject]stringbyappendingpathcomponent:@ "1.jpg"];    Nsdictionary *dict=[[nsfilemanager Defaultmanager]attributesofitematpath:path Error:NULL]; return [dict[nsfilesize] longlongvalue];} -(void) Downloadwithurl: (nsurl *) URL Fromb: (Long Long) Fromb ToB: (Long Long) tob{nsmutableurlrequest *request=[    Nsmutableurlrequest Requestwithurl:url Cachepolicy:nsurlrequestreloadignoringcachedata timeoutInterval:2.0f]; NSString *range=[nsString stringwithformat:@ "Bytes=%lld-%lld", Fromb,tob];    [Request Setvalue:range forhttpheaderfield:@ "range"];    Nsurlresponse *response=nil;        NSData *data=[nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];    Write file [self appenddata:data]; NSLog (@ "%@", Response);} -(void) AppendData: (NSData *) data{nsstring *path=[[nssearchpathfordirectoriesindomains (nscachesdirectory,    Nsuserdomainmask, YES) lastobject]stringbyappendingpathcomponent:@ "1.jpg"];    Nsfilehandle *fh=[nsfilehandle Filehandleforwritingatpath:path];    if (!FH) {[Data writetofile:path atomically:yes];        }else{[FH Seektoendoffile];        [FH Writedata:data];    [FH CloseFile]; }} @end


"iOS Dev-98" File download: @ "HEAD" and [Request Setvalue:range forhttpheaderfield:@ "range"]

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.