1. Method One: If the NSData data is large, put a large amount of data into memory, memory will definitely explode, so this method is not advisable
Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://localhost/123.jpg"];
Set Request
Self.request = [ASIHTTPRequest Requestwithurl:url];
Receive the data returned by the server
[Self.request setdatareceivedblock:^ (NSData *data) {
NSLog (@ "-----%d", data.length);
}];
Initiating a request
[Self.request startasynchronous];
2. Method Two:
Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://localhost/123.jpg"];
Set Request
Self.request = [ASIHTTPRequest Requestwithurl:url];
Set the path where the file is stored locally
NSString *cache = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];
NSString *cachepath = [Cache stringbyappendingpathcomponent:@ "Cache.db"];
[Self.request Setdownloaddestinationpath:cachepath];
Called when the download is complete
[Self.request setcompletionblock:^{
NSLog (@ "Download, with the ASI download, is so easy to get it done!");
}];
Initiating a request
[Self.request startasynchronous];
3. Listen to the download progress, first abide by the Asiprogressdelegate protocol
//Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://localhost/123.jpg"];
//Setup Request
Self.request = [ASIHTTPRequest Requestwithurl:url];
//Set the path where the file is stored locally
NSString *cache = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];
NSString *cachepath = [Cache stringbyappendingpathcomponent:@ "Cache.db"];
[Self.request Setdownloaddestinationpath:cachepath];
Set Download Progress Agent
Self.request.downloadProgressDelegate = self;
//Receive data returned by the server
[Self.request setcompletionblock:^{
NSLog (@ "Download finished");
}];
//initiating a request
[Self.request startasynchronous];
}
#pragma mark-asiprogressdelegate
-(void) setprogress: (float) newprogress{
NSLog (@ "Download progress:%f", newprogress);
}
Add: If you can get the progress bar control, listen to the download progress there is a way to
Self.request.downloadProgressDelegate = Self.progressview;
/* Proxy method can omit to write, because the Self.progressview has a progress attribute, meaning that Progressview internal has implemented the progress set method, Just Downloadprogressdelegate proxy method is setprogress:, so the following code can be omitted
-(void) setprogress: (float) newprogress{
Self.progressView.progress = newprogress
} */
4. Support for continuous transmission of breakpoints [Self.request Setallowresumeforfiledownloads:yes]; Add this code and it's OK.
Principle: If there is a button, click the button to execute this method [Self.request Cleardelegatesandcancel]; The ASI will then put the downloaded data into the sandbox cache, and the next time it is downloaded, the new data will be automatically connected to the downloaded old data.
IOS asi--File Download