Nsurlsession implementing file Uploads

Source: Internet
Author: User

7.1 Related to knowledge points

(1) How to implement file upload
/*      First parameter: Request object     second parameter: request body (file data to upload)     block callback:     NSData: Response body     nsurlresponse: Response header     Nserror: Requested error message      *    /*uploadtask =  [Session uploadtaskwithrequest:request Fromdata:data completionhandler:^ (NSData * __nullable data, Nsurlresponse * __nullable response, Nserror * __nullable Erro R

(2) Set up Agent to listen for file upload progress in Agent method
/* call this method to upload the file data if the file data is large, then the method is called multiple parameter descriptions:     totalbytessent: The size     of the file data that has been uploaded */-(void) Urlsession: (nonnull nsurlsession *) session task: (nonnull nsurlsessiontask *) Task Didsendbodydata: (int64_t) BytesSent totalbytessent: (int64_t) totalbytessent totalbytesexpectedtosend: (int64_t) totalBytesExpectedToSend{    NSLog (@ "%.2f",1.0 * totalbytessent/  totalbytesexpectedtosend);}

(3) About nsurlsessionconfiguration related

01 functions: Nsurlsession can be configured uniformly, such as request timeout, etc.
02 Ways to create and use

//three ways to create a configuration+ (Nsurlsessionconfiguration *) defaultsessionconfiguration;+ (Nsurlsessionconfiguration *) ephemeralsessionconfiguration;+ (Nsurlsessionconfiguration *) Backgroundsessionconfigurationwithidentifier: (NSString *) identifier ns_available (10_10, 8_0);//Unified Configuration Nsurlsession-(Nsurlsession *) session{if(_session = =Nil) {        //Create NsurlsessionconfigurationNsurlsessionconfiguration *config =[Nsurlsessionconfiguration defaultsessionconfiguration]; //Set request Timeout to 10 secondsConfig.timeoutintervalforrequest =Ten; //whether to continue the request (upload or download) in the case of a cellular networkConfig.allowscellularaccess =NO; _session= [Nsurlsession sessionwithconfiguration:configDelegate: Self delegatequeue:[nsoperationqueue mainqueue]]; }    return_session;}

Nsurlsession implementing file Uploads

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.