ASIHTTPRequest implements resumable upload

Source: Internet
Author: User

ASIHTTPRequest enables resumable upload. There are some introductions on the Internet, such as: [request setAllowResumeForFileDownloads: YES]; method. However, it is not a resumable upload. It can only pause and continue the application during the download process. It is invalid if you exit the application and enter again. However, ASIHTTPRequest asynchronous requests and delegate can still implement resumable data transfer. This article uses Grails as an example to compile the resumable data transfer server. Code of the asynchronous request:-(void) doSimpleGetBinary {NSURL * url = [NSURL URLWithString: @ "http: // localhost: 8080/BookProto/book/image"]; ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url]; [request setRequestMethod: @ "GET"]; // [request addRequestHeader: @ "Range" value: @ "bytes = 3-"]; [request setDelegate: self]; [request startAsynchronous];} Delegate is set here. The corresponding protocol: @ interface CFHttpDemoViewControl must be implemented in the header file. Ler: UIViewController <ASIProgressDelegate> {The following delegate method is used in this example. RequestFinished:-(void) requestFinished :( ASIHTTPRequest *) request {NSLog (@ "response status code: % I", [request responseStatusCode]); NSLog (@ "response content length: % @ ", [[request responseHeaders] objectForKey: @" Content-Length "]); NSLog (@" request finished. "); label. text = @ "request finished. ";} this method is called after the asynchronous request ends. The following method is called after the buffer zone receives part of the data. It looks like every interval of milliseconds. Call the method and pass in the NSData object of the buffer zone. -(Void) request :( ASIHTTPRequest *) request didReceiveData :( NSData *) data {NSLog (@ "did receive data, data length: % I", [data length]); // copy Byte * byteData = (Byte *) malloc ([data length]) to the Byte array; memcpy (byteData, [data bytes], [data length]); for (int I = 0; I <= 10; I ++) {NSLog (@ "% I: % I", I + 1, byteData [I]);} free (byteData); [request cancel]; label. text = @ "canceled. ";} run the code. Blocking: [request addRequestHeader: @" Range "Value: @" bytes = 3-"]; and unblocking. The data is as follows: 14:17:13. 497 CFHttpDemo [2647: 207] did receive data, data length: 10172 14:17:13. 514 CFHttpDemo [2647: 207] 1: 137 14:17:13. 515 CFHttpDemo [2647: 207] 2: 80 14:17:13. 516 CFHttpDemo [2647: 207] 3: 78 14:17:13. 516 CFHttpDemo [2647: 207] 4: 71 14:17:13. 517 CFHttpDemo [2647: 207] 5: 13 14:17:13. 518 CF HttpDemo [2647: 207] 6: 10 14:17:13. 518 CFHttpDemo [2647: 207] 7: 26 14:17:13. 519 CFHttpDemo [2647: 207] 8: 10 14:17:13. 520 CFHttpDemo [2647: 207] 9: 0 14:17:13. 520 CFHttpDemo [2647: 207] 10: 0 14:17:13. 521 CFHttpDemo [2647: 207] 11: 0 14:17:13. 522 CFHttpDemo [2647: 207] response status code: 200 14:17:13. 523 CFHttpDemo [2647: 207] respo Neuron content length: 10172 14:17:13. 523 CFHttpDemo [2647: 207] request finished. 14:02:24. 551 CFHttpDemo [2578: 207] did receive data, data length: 10169 14:02:24. 553 CFHttpDemo [2578: 207] byteData OK. 14:02:24. 554 CFHttpDemo [2578: 207] 1: 71 14:02:24. 554 CFHttpDemo [2578: 207] 2: 13 14:02:24. 555 CFHttpDemo [2578: 207] 3: 10 4.555 CFHttpDemo [2578: 207] 4: 26 14:02:24. 556 CFHttpDemo [2578: 207] 5: 10 14:02:24. 556 CFHttpDemo [2578: 207] 6: 0 14:02:24. 557 CFHttpDemo [2578: 207] 7: 0 14:02:24. 557 CFHttpDemo [2578: 207] 8: 0 14:02:24. 558 CFHttpDemo [2578: 207] 9: 13 14:02:24. 558 CFHttpDemo [2578: 207] 10: 73 14:02:24. 560 CFHttpDemo [2578: 207] 11: 72 14:02:24. 561 CFHttpDemo [2578: 207] response status code: 206 14:02:24. 561 CFHttpDemo [2578: 207] response content length: 10169 14:02:24. 562 CFHttpDemo [2578: 207] request finished. 2. the key to implementing resumable data transfer through NSUrlConnection is to customize the range field attribute of the http request header. The Range header field can request one or more sub-ranges of an object. For example, it indicates the first 500 bytes: bytes = 0-499 indicates the second 500 bytes: bytes = 500-999 indicates the last 500 bytes: bytes =-500 indicates the range after 500 bytes: bytes = 500-first and last bytes: bytes = 0-0,-1 specifies the range at the same time: bytes = 500-600,601-999 but the server can ignore this request header. If the unconditional GET contains the Range request header, the response will be returned with the status code 206 (PartialContent) instead of 200 (OK ). In ios, NSMutableURLRequest is used to define the header domain NSURL * url1 = [NSURL URLWithString: @ ""; required * request1 = [NSMutableURLRequest requestWithURL: url1]; [request1 setValue: @ "bytes = 20000-" bytes: @ "Range"]; [request1 setCachePolicy: bytes]; NSData * returnData1 = [NSURLConnection sendSynchronousRequest: request1 returningResponse: nil error: nil]; [self writeToFile: r EturnData1 fileName: @ "SOMEPATH"];-(void) writeToFile :( NSData *) data fileName :( NSString *) fileName {NSString * filePath = [NSString stringWithFormat: @ "% @", fileName]; if ([[NSFileManager defaultManager] fileExistsAtPath: filePath] = NO) {NSLog (@ "file not exist, create it... "); [[NSFileManager defaultManager] createFileAtPath: filePath contents: nil attributes: nil];} else {NSLog (@" file exist !!! ");} FILE * file = fopen ([fileName UTF8String], [@" AB + "UTF8String]); if (file! = NULL) {fseek (file, 0, SEEK_END);} int readSize = [data length]; fwrite (const void *) [data bytes], readSize, 1, file); fclose (file );}

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.