IOS asi--File Download

Source: Internet
Author: User

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

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.