#import "ViewController.h"@interfaceViewcontroller () <NSURLConnectionDataDelegate>@property (Weak, nonatomic) Iboutlet Uiprogressview*Progressview; @property (nonatomic, assign) Nsinteger totalsize; @property (nonatomic, assign) Nsinteger currentsize ;/** File handle*/@property (nonatomic, strong) Nsfilehandle*handle;/** Sandbox path*/@property (nonatomic, strong) NSString*FullPath;/** Connection Object*/@property (nonatomic, strong) Nsurlconnection*Connect;@end@implementationViewcontroller-(Ibaction) Startbtnclick: (IDsender {[SELF download];}-(Ibaction) Cancelbtnclick: (IDSender {[self.connect cancel];}-(Ibaction) Goonbtnclick: (IDsender {[SELF download];}//Memory Spike-(void) download{//1.url//Nsurl *url = [Nsurl urlwithstring:@]http://imgsrc.baidu.com/forum/w%3D580/sign=54a8cc6f728b4710ce2ffdc4f3cec3b2/ D143ad4bd11373f06c0b5bd1a40f4bfbfbed0443.jpg"];Nsurl*url = [Nsurl urlwithstring:@"http://www.33lc.com/article/UploadPic/2012-10/2012102514201759594.jpg"]; //2. Create a Request objectNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; //set the request header information to tell the server value to request part of the data range /*bytes=0-100 bytes=-100 bytes=0-all data after request 100*/NSString*range = [NSString stringWithFormat:@"bytes=%zd-", Self.currentsize]; [Request Setvalue:range Forhttpheaderfield:@"Range"]; NSLog (@"+++++++%@", Range); //3. Sending the requestNsurlconnection *connect = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self]; Self.connect=Connect;}#pragmaMark----------------------#pragmaMark Nsurlconnectiondatadelegate-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@"Didreceiveresponse"); //1. Get the total size of the file (total size of file data for this request! = Total size of file)//self.totalsize = response.expectedcontentlength + self.currentsize; if(Self.currentsize >0) { return; } self.totalsize=response.expectedcontentlength; //2. Write the data into the sandboxSelf.fullpath = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject] stringByAppendingPathComponent:@"123.jpg"]; NSLog (@"%@", Self.fullpath); //3. Create an empty file[[Nsfilemanager Defaultmanager] CreateFileAtPath:self.fullPath contents:nil Attributes:nil]; //nsdictionary *dict = [[Nsfilemanager defaultmanager]attributesofitematpath:self.fullpath Error:nil]; //4. Create a file handle (pointer)Self.handle =[Nsfilehandle FileHandleForWritingAtPath:self.fullPath];}-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//1. Move the file handle to the end of the data[Self.handle Seektoendoffile]; //2. Write Data[Self.handle Writedata:data]; //3. Get ProgressSelf.currentsize + =data.length; //progress = total size of downloaded/fileNSLog (@"%f",1.0* self.currentsize/self.totalsize); Self.progressView.progress=1.0* self.currentsize/self.totalsize; //NSLog (@ "%@", Self.fullpath);}-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{}-(void) Connectiondidfinishloading: (Nsurlconnection *) connection{//1. Close the file handle[Self.handle CloseFile]; Self.handle=Nil; NSLog (@"connectiondidfinishloading"); NSLog (@"%@", Self.fullpath);}@end
# # # # # #6 0 Large file breakpoint download
(1) Realization idea
When downloading a file, it is no longer downloaded from the beginning of the whole block, but rather to the place where the current file has been downloaded and then from there to the back. You can set the request header implementation by using the Request object.
(2) Solution (set request header)
```
2. Create a Request object
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
2.1 Set up a portion of the download file
As long as you set the Range property of the HTTP request header, you can start the download from the specified location
/*
Represents the first 500 bytes: range:bytes=0-499
Represents a second 500 byte: range:bytes=500-999
Represents the last 500 bytes: range:bytes=-500
Represents the range after 500 bytes: range:bytes=500-
*/
NSString *range = [NSString stringwithformat:@ "bytes=%zd-", self.currentlength];
[Request Setvalue:range forhttpheaderfield:@ "range"];
```
(3) Note (Download progress and determine if you need to recreate the file)
"' OBJC
Gets the total size of the file currently being downloaded (from the response header)
Nshttpurlresponse *res = (Nshttpurlresponse *) response;
Note: Res.expectedcontentlength Gets the size of the file that this request is being downloaded (not the full file size)
Therefore: The total size of the FILE = = The file size of the secondary download + the size of the file that has already been downloaded
Self.totallength = Res.expectedcontentlength + self.currentlength;
NSLog (@ "----------------------------%zd", self.totallength);
0 determine if the current file has already been downloaded, and if it already exists, return directly
if (Self.currentlength >0) {
Return
}
```
iOS Development Network Learning Four: nsurlconnection large file breakpoint download