This article introduces the content of ASIHTTPRequest breakpoint resume (download) for iOS development, including ASIHTTPRequest can resume interrupted downloads and set a temporary download path, the working principle of resumable upload and so on.
From version 0.94, ASIHTTPRequest can resume interrupted downloads.
This feature is only valid for downloading data to files. You must set allowResumeForFileDownloads to YES for the following request:
- Any download that you want to resume in the future. Otherwise, ASIHTTPRequest will delete the temporary files when canceling or releasing the memory)
- Any download for resumable upload
In addition, you must set a temporary download path setTemporaryFileDownloadPath), which is the path of unfinished data. New data will be added to this file. When the download is complete, the file will be moved to downloadDestinationPath.
- -(IBAction) resumeInterruptedDownload :( id) sender
- {
- NSURL * url = [NSURL URLWithString:
- @ Http://www.dreamingwish.com/wp-content/uploads/2011/10/asihttprequest-auth.png];
- ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
- NSString * downloadPath = @ "/Users/ben/Desktop/asi.png ";
- // When the request is complete, the entire file will be moved here
- [Request setDownloadDestinationPath: downloadPath];
- // This file has been downloaded.
- [Request setTemporaryFileDownloadPath: @ "/Users/ben/Desktop/asi.png. download"];
- [Request setAllowResumeForFileDownloads: YES];
- [Request startSynchronous];
- // The entire file will be here
- NSString * theContent = [NSString stringWithContentsOfFile: downloadPath];
- }
The principle of resumable upload is to read the file size of temporaryFileDownloadPath and request the remaining file content using the Range: bytes = x HTTP header.
ASIHTTPRequest does not check whether the Accept-Ranges header exists. Because the additional HEAD header request consumes additional resources, this feature is used only when the server determines that resumable download is supported.