IOS submit Form form, upload image

Source: Internet
Author: User

Not used before, always feel very difficult to use after feeling is not too difficult, this article is just a simple talk about how to use,

//instance Session ObjectAfhttpsessionmanager *manager =[Afhttpsessionmanager Manager]; Manager.requestSerializer.timeoutInterval= -; Manager.responseserializer=[[Afhttpresponseserializer alloc] init]; Manager.responseSerializer.acceptableContentTypes= [Nsset setwithobjects:@"Application/json",@"Text/json",@"Text/javascript",@"text/html", nil]; //Add the parameters you need in the request headerNSString *token =[[Yxloginstatustool Sharedloginstatus] Gettokenid]; [Manager.requestserializer setvalue:token Forhttpheaderfield:@"ID"];//Request AddressNSString *urlstr =@""; [Manager post:urlstr Parameters:nil Constructingbodywithblock:^(ID<AFMultipartFormData>_nonnull FormData) {

Turn into data

Self.justimagedata = Uiimagepngrepresentation (pickerimagepic);

//write binary files into formdata form in form format[FormData appendPartWithFileData:self.justimagedata Name:@""FileName:@""MimeType:@"Image/png"]; [FormData appendPartWithFileData:self.backimagedata Name:@""FileName:@""MimeType:@"Image/png"]; } Progress:^ (Nsprogress *_nonnull uploadprogress) {Yxlog (@"++++%@+++", uploadprogress); } Success:^ (Nsurlsessiondatatask *task,IDresponseobject) {Yxlog (@"Responseobject = =%@--++--%@----", [self getresponseobjcwithtask:responseobject],[self getrespodheaderwithtask:task]); if([[Self getrespodheaderwithtask:task][@"Status"] Isequaltostring:@"1"]) {                       //Success}Else{[Yxalearmnager showalearviewwith:[self getrespodheaderwithtask:responseobject][@"errormsg"] Type:2];            } [Yxnetworkhud dismiss]; } Failure:^ (Nsurlsessiondatatask *task, Nserror *error)                 {[Yxnetworkhud dismiss]; Yxlog (@"Send failed +++++%@++++%@", error,[self Getrespodheaderwithtask:task]); [Yxalearmnager Showalearviewwith:@"Avatar Replacement failed"Type:2]; }];
//* * Get response header */- (ID) Getrespodheaderwithtask: (Nsurlsessiontask *) task{Nshttpurlresponse*respond = (Nshttpurlresponse *) Task.response; returnRespond.allheaderfields;}-(ID) Getresponseobjcwithtask: (ID) responseobect{IDJSON; Nserror*error; JSON= [Nsjsonserialization jsonobjectwithdata:responseobect options:0error:&ERROR]; if(Error) {JSON=[[NSString alloc] Initwithdata:responseobect encoding:nsutf8stringencoding]; }    returnJSON;}

IOS submit Form form, upload image

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.