objective c - How to upload image to server with headers in ios -


i'm trying upload image server headers hope code fine don't know cannot upload. code

{      nsdictionary *headers = @{ @"content-type":@"multipart/form-data; boundary=----webkitformboundary7ma4ywxktrzu0gw",                                    @"p-auth-token":self.token};      nsstring *urlstring = [ nsstring stringwithformat:@"http://ica.com/facilitator/server/v1/media"];      uiimage *image= profile_image;     nsdata *imagedata = uiimagejpegrepresentation(image, 0.1);     double my_time = [[nsdate date] timeintervalsince1970];     nsstring *imagename = [nsstring stringwithformat:@"%d",(int)(my_time)];     nsstring *string = [nsstring stringwithformat:@"%@%@%@", @"content-disposition: form-data; name=\"file\"; filename=\"", imagename, @".jpg\"\r\n\""];     nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init];     [request seturl:[nsurl urlwithstring:urlstring]];     [request setallhttpheaderfields:headers];     [request sethttpmethod:@"post"];      nsstring *boundary = @"---------------------------14737809831466499882746641449";     nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary];     [request addvalue:contenttype forhttpheaderfield: @"content-type"];      nsmutabledata *body = [nsmutabledata data];     [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]];     [body appenddata:[[nsstring stringwithstring:string] datausingencoding:nsutf8stringencoding]];      [body appenddata:[@"content-type: application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]];     [body appenddata:[nsdata datawithdata:imagedata]];     [body appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n",boundary] datausingencoding:nsutf8stringencoding]];     [request sethttpbody:body];       nsurlsession *session = [nsurlsession sharedsession];      [[session datataskwithrequest:request completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) {          nsdictionary *statusdict = [[nsdictionary alloc]init];         statusdict = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil];           [svprogresshud dismiss];             nslog(@"images form server %@", statusdict);        }] resume]; } 

try afnetworking below code,

nsmutableurlrequest *request = [[afhttprequestserializer serializer] multipartformrequestwithmethod:@"post" urlstring:@"upload_url" parameters:nil constructingbodywithblock:^(id<afmultipartformdata> formdata) {      [formdata appendpartwithfiledata:data name:@"upload_file_name" filename:@"somefilename.jpg" mimetype:@"mime_type_of_file"] // file upload      } error:nil];  afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]];  nsurlsessionuploadtask *uploadtask; uploadtask = [manager     uploadtaskwithstreamedrequest:request     progress:^(nsprogress * _nonnull uploadprogress) {     // not called on main queue.     // responsible dispatching main queue ui updates     dispatch_async(dispatch_get_main_queue(), ^{     //update progress view     [progressview setprogress:uploadprogress.fractioncompleted];     });     }     completionhandler:^(nsurlresponse * _nonnull response, id  _nullable responseobject, nserror * _nullable error) {     if (error) {     nslog(@"error: %@", error);     } else {     nslog(@"%@ %@", response, responseobject);     }     }];  [uploadtask resume]; 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -