1. Download small files, only for hundreds of k,1, 2M files
1. Set URL//2. Load request//3. Set proxy//4. Implement proxy method Download file Nsurl *url = [Nsurl urlwithstring:@ "https://timgsa.baidu.com/timg?image& Quality=80&size=b9999_10000&sec=1492791082124&di=fc642407b4ec19430334653a9b873cff&imgtype=0 &src=http%3a%2f%2fi0.szhomeimg.com%2fuploadfiles%2fbbs%2f2006%2f10%2f24%2f27567833_1950.442.jpg "]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; [[Nsurlconnection alloc] initwithrequest:request delegate:self]; Agent implementation #pragma mark---nsurlconnectiondelegate-(void) Connection: (nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "Download failed%@", error);} Download response-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) response{//Get File Size _ TotalSize = Response.expectedcontentlength;} Download data-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//Stitching data [_mudata Appenddata:dat A]; Gets the current progress NSLog (@ "%f", 1.0*_mudata.length/_totalsize);} Download complete-(void) connectiondidfinishloading: (nsurlconnectiOn *) connection{nsstring *fullpath = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDomainMask, YE S) Lastobject] stringbyappendingpathcomponent:@ "1.jpg"]; [_mudata Writetofile:fullpath atomically:yes];}
Cons: [_mudata appenddata:data], memory spikes, and the memory does not drop after download
2. Use the handle to download large files
#pragma mark----------------------#pragma mark nsurlconnectiondatadelegate-(void) connection: (Nsurlconnection *) Connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@ "Didreceiveresponse"); 1. Get the total size of the file (the total size of the file data for this request) Self.totalsize = Response.expectedcontentlength; 2. Write the data into the sandbox self.fullpath = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]stringbyappendingpathcomponent:@ "123.mp4"]; 3. Create an empty file [[Nsfilemanager Defaultmanager] CreateFileAtPath:self.fullPath contents:nil Attributes: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 Seektoe Ndoffile]; 2. Write data [Self.handle Writedata:data]; 3. Get Progress Self.currentsize + = Data.length; Progress = The total size of the downloaded/file NSLog (@ "%f", 1.0 * self.currentsize/self.totalsize); Self.progressview.progress = 1.0 * SELF.CURRENTSIZE/SELF.TOTALSIZE; NSLog (@ "%@", Self.fullpath);} -(void) connectiondidfinishloading: (nsurlconnection *) connection{//1. close file handle [Self.handle CloseFile]; Self.handle = nil; NSLog (@ "connectiondidfinishloading"); NSLog (@ "%@", Self.fullpath);}
3. Download large files via breakpoints
Set the request header to continue downloading if the file exists
Ibid .//2. Create Request Object nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url]; NSString *range = [NSString stringwithformat:@ "bytes=%zd-", self.currentsize]; [Request Setvalue:range forhttpheaderfield:@ "range"]; Ditto} #pragma mark----------------------#pragma mark nsurlconnectiondatadelegate-(void) connection: (nsurlconnection *) connection Didreceiveresponse: (Nsurlresponse *) response{ //1. Gets the total size of the file (the total size of the file data for this request) Self.totalsize = Response.expectedcontentlength+self.currentsize; if (Self.currentsize > 0) { return; } Ibid-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{ //Ibid.}-(void) Connectiondidfinishloading: (nsurlconnection *) connection{ //Ibid.}
Cons: The next time you run the project, you should download it again
File download nsurlconnection--small file download