iOS Development Network Programming--5, nsurlsessionuploadtask+nsurlsessiondatadelegate agent upload

Source: Internet
Author: User

Preface: The main content about Nsurlsession is coming to an end, here is to say file upload. About file upload of course to use Nsurlsessionuploadtask, here directly to explain the common will and agent nsurlsessiondatadelegate together to achieve file upload function. In addition, the following use of the file upload idea is and nsurlconnection in my previous essay "IOS Development Network programming-using Nsurlconnection to achieve file upload" mentioned in the upload idea is the same, are to be the request information splicing up, It is then passed in to the request for uploading. This splicing process is necessary, but is also more cumbersome, below I will not be too much to explain the details. In addition, the use of agents is relatively simple, so directly display all the source code.

1 #import "ViewController.h"2 #defineKboundary @ "----webkitformboundary0iqat0ha7oxwix3f"3 #defineknewline [@ "\ r \ n" datausingencoding:nsutf8stringencoding]4 5 @interfaceViewcontroller () <NSURLSessionDataDelegate>6 7 @end8 9 @implementationViewcontrollerTen  One-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event A { -NSLog (@"------"); -     //1. Create the Session object, set the proxy theNsurlsession *session = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]Delegate: Self delegatequeue:[nsoperationqueue mainqueue]]; -      -     //2. Create a Request object -Nsurl *url =[nsurl urlwithstring:@"Http://120.25.226.186:32812/upload"]; +Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; -      +     //2.1 Modify Request Method ARequest. HttpMethod =@"POST"; at      -     //2.2 Setting the request header -NSString *header = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kboundary]; -[Request Setvalue:header Forhttpheaderfield:@"Content-type"]; -      -     //3. Create an upload task in     /* - first parameter: Request Object to second parameter: parameter to upload a file (binary data + third parameter: Completionhandler - data: Results returned by the server (response body information) the Response: Response header *      */ $Nsurlsessionuploadtask *uploadtask = [session uploadtaskwithrequest:request fromdata:[self GetBodyData] completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable response, Nserror *_nullable Error) {Panax Notoginseng         -        //5. Parsing results theNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); +     }]; A      the     //4. Perform Tasks + [Uploadtask resume]; -      $ } $  --(NSData *) Getbodydata - { the     //5. Stitching Data -Nsmutabledata *filedata =[Nsmutabledata data];Wuyi      the     //5.1 Stitching file Parameters -     /* Wu --Separators - content-disposition:form-data; name= "file"; filename= "Snip20151228_572.png" About Content-type:image/png $ Blank Line - File binary Data -      */ -[FileData appenddata:[[nsstring stringWithFormat:@"--%@", Kboundary] datausingencoding:nsutf8stringencoding]]; A [FileData appenddata:knewline]; +     //name= "file": parameter, is fixed the     //FileName: The file is uploaded to the server by what name to save, casually -[FileData appenddata:[@"content-disposition:form-data; name=\"File\"; filename=\ "snip20151228_572.png\""Datausingencoding:nsutf8stringencoding]]; $      the [FileData appenddata:knewline]; the     //Content-type: The type of file to upload (MIMEType) the[FileData appendData: [@"Content-type:image/png"Datausingencoding:nsutf8stringencoding]]; the [FileData appenddata:knewline]; - [FileData appenddata:knewline]; in      theUIImage *image = [UIImage imagenamed:@"snip20151229_713"]; theNSData *imagedata =uiimagepngrepresentation (image); About [FileData Appenddata:imagedata]; the [FileData appenddata:knewline]; the      the     //5.2 Stitching Non-file parameters +     /* - --Separators the content-disposition:form-data; name= "username"Bayi Blank Line the Binary data for non-file parameters the      */ -[FileData appenddata:[[nsstring stringWithFormat:@"--%@", Kboundary] datausingencoding:nsutf8stringencoding]]; - [FileData appenddata:knewline]; the      the     //Username: The same file is the server regulation the[FileData appenddata:[@"content-disposition:form-data; name=\"Username\""Datausingencoding:nsutf8stringencoding]]; the [FileData appenddata:knewline]; - [FileData appenddata:knewline]; the[FileData appenddata:[@"Dashen9"Datausingencoding:nsutf8stringencoding]]; the [FileData appenddata:knewline]; the     94     //5.3 Stitching End logo the     /* the -- delimiter-- the      */98[FileData appenddata:[[nsstring stringWithFormat:@"--%@--", Kboundary] datausingencoding:nsutf8stringencoding]]; About      -     returnFileData;101 }102 103 #pragmaMark----------------------104 #pragmaMark Nsurlsessiondatadelegate the /*106 first parameter: bytessent the size of the data sent this time107 the second parameter: totalbytessent how much data is sent altogether108 Third parameter: Total size of totalbytesexpectedtosend file109  */ the-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didsendbodydata: (int64_t) bytessent totalbytessent: (int64_t) totalbytessent totalbytesexpectedtosend: ( int64_t) Totalbytesexpectedtosend111 { theNSLog (@ "already uploaded data for%f",1.0* totalbytessent/totalbytesexpectedtosend);113 } the @end

iOS Development Network Programming--5, nsurlsessionuploadtask+nsurlsessiondatadelegate agent upload

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.