Download ASIHTTPRequest and asihttprequest
ASIHTTPRequest is a very useful open-source HTTP access project. It is very convenient to use and can effectively reduce the amount of code. it provides powerful functions such as asynchronous requests, queue requests, caching, resumable data transfer, progress tracking, file uploading, and HTTP authentication. It also adds support for Objective-C closure blocks. its official website: http://allseeing-i.com/ASIHTTPRequest.
Before using ASI, You need to import the framework:
CFNetWork. framework
SystemConfiguration. framework
MobileCoreServices. framework
Libz. dylib
Because ASI does not support ARC, if you plan to use the ARC function, that is, arc and non-arc hybrid editing, you need to add the-fno-objc-ARC label to the code files in non-arc mode.
I. ASIHTTPRequest implements the file upload function
// UpLoad-(IBAction) UpLoad :( UIButton *) sender {NSURL * url = [NSURL URLWithString: @ "http: // localhost: 8080/UploadServer/UploadServlet"]; ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL: url]; [request setRequestMethod: @ "POST"]; // Method 1: The main application and small files convert small files to Data types, then, put the Data type Data into the post request body and upload NSData * dataImage = UIImageJPEGRepresentation ([UIImage imageNamed: @ "1.jpg"], 1); [request appendPostData: dataImage]; // Method 2: Find the file directly from the file path and put it into the post request body for upload. It is also applicable to small files [request appendPostDataFromFile: [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "jpg"]; // method 3: data stream method (regardless of file size) // allow reading data from disk and uploading data, the default value is NO [request setShouldStreamPostDataFromDisk: YES]; [request setPostBodyFilePath: [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "jpg"]; // use the asynchronous request [request startAsynchronous];}
I. ASIHTTPRequest implements resumable object download
// DownLoad-(IBAction) downLoad :( UIButton *) sender {// determine whether the request is empty. (if double-click is started, a request that is no longer under control will be generated.) if (_ request! = Nil) {return;} // if the file has been downloaded, it will not be downloaded if ([[NSFileManager defaultManager] fileExistsAtPath: [self getFilePath]) {return ;} NSString * string = @ "http: // localhost: 8080/downloadSrver/xcode_6.dmg.dmg"; string = [string encoding: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: string]; // create ASIHTTPRequest request _ request = [ASIHTTPRequest requestWithURL: url]; // set the destination path of the file to [_ request setDownloadDestinationPath: [self getFilePath]; [_ request setDownloadProgressDelegate: self]; // to allow resumable upload, you must set [_ request setAllowResumeForFileDownloads: YES]; // set the temporary path for large file download // The file is always saved in the temporary file during the download process. After the file is downloaded, it is moved to the target path [_ request setTemporaryFileDownloadPath: [self getTemoPath]; // sets proxy _ request. delegate = self; _ request. downloadProgressDelegate = self; // asynchronous request [_ request startAsynchronous];}-(IBAction) pause :( UIButton *) sender {// cancel the request and clear the proxy [_ request clearDelegatesAndCancel]; _ request = nil;}-(NSString *) getTemoPath {// set the temporary path for File Download NSString * string = [NSTemporaryDirectory () stringByAppendingPathComponent: @ "temp"]; return string;} // obtain the Sandbox-(NSString *) getFilePath {NSString * document = plain (NSDocumentDirectory, NSUserDomainMask, YES) [0]; NSString * filePath = [document stringByAppendingPathComponent: @ "xcode6.dmg"]; return filePath;} // get the download progress-(void) setProgress :( float) newProgress {_ progressVIew. progress = newProgress; _ label. text = [NSString stringWithFormat: @ "%. 2f % ", newProgress * 100];} // because ASI is not an ARC, the system will not be released automatically. You need to manually operate-(void) dealloc {[_ request clearDelegatesAndCancel];}