ios - Post multiple images into web server -


the code make me happy post image web server. working smart single image. code i've used post single image is

nslog(@"%@",requeststring);  nsstring *url=[nsstring stringwithformat:@"http://37.187.152.236/userimage.svc/insertfacialimage?%@",requeststring]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init] ; [request seturl:[nsurl urlwithstring:url]]; [request sethttpmethod:@"post"];  // create 'post' mutablerequest data , other image attachment.  nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", boundary]; [request setvalue:contenttype forhttpheaderfield:@"content-type"]; nsdata *data = uiimagejpegrepresentation(chosenimage, 0.2f); [request addvalue:@"image/jpeg" forhttpheaderfield:@"content-type"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[nsdata datawithdata:data]]; [request sethttpbody:body]; 

then failed jump on hurdle, failed post 2 images different services.the truth behind failure after uploading of 1st image, server generate response i've attach response second service. did failed because made connection run 2 times 2 images.but web services team asking me run in single connection.the code used, need modify

posting 1st image

 nsstring *url=[nsstring stringwithformat:@"http://37.187.152.236/userimage.svc/insertobjectimage?%@",requeststring];   nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init] ; [request seturl:[nsurl urlwithstring:url]]; [request sethttpmethod:@"post"];  // create 'post' mutablerequest data , other image attachment.  nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", boundary]; [request setvalue:contenttype forhttpheaderfield:@"content-type"]; nsdata *data = uiimagejpegrepresentation(chosenimage1, 0.2f); [request addvalue:@"image/jpeg" forhttpheaderfield:@"content-type"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[nsdata datawithdata:data]]; [request sethttpbody:body]; nsdata *returndata; nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding];  nslog(@"ret: %@",returnstring);  nsurlconnection *connreq = [nsurlconnection connectionwithrequest:request delegate:self];  if (connreq) {      nslog(@"connection sucessful");     receiveddata = [[nsmutabledata alloc]init];  }   else {      nslog(@"failed");  }  nshttpurlresponse *response = nil; nserror *error = nil; nsdata *respdata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nslog(@"status code: %ld", (long)[response statuscode]); 

generating response

-(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data{ [self.receiveddata appenddata:data]; 

}

-(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error{  nslog(@"%@" , error); 

}

-(void)connectiondidfinishloading:(nsurlconnection *)connection{ nserror *e;    jsondict1 = [nsjsonserialization jsonobjectwithdata:receiveddata options: nsjsonreadingmutablecontainers error: &e];  compareid = [jsondict1 valueforkey:@"imageid"]; 

nslog(@"jsonn%@" , jsondict1);

compareid = [results.firstobject objectforkey:@"imageid"];   nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; [defaults setobject:self.compareid forkey:@"sendy"]; [defaults synchronize];  nslog(@"compareid:%@",compareid);   results = [nsjsonserialization jsonobjectwithdata:receiveddata options:nsjsonreadingmutablecontainers error:&e];    } 

}

after step response generating , can pass second string. no problem server response.

*post 2nd image *

nsstring *url1=[nsstring stringwithformat:@"http://37.187.152.236/userimage.svc/updateobjectimage?%@",requeststring1];  nsmutableurlrequest *request1 = [[nsmutableurlrequest alloc] init] ; [request1 seturl:[nsurl urlwithstring:url1]]; [request1 sethttpmethod:@"post"];  // create 'post' mutablerequest data , other image attachment.  nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", boundary]; [request1 setvalue:contenttype forhttpheaderfield:@"content-type"]; nsdata *data = uiimagejpegrepresentation(chosenimage2, 0.2f); [request1 addvalue:@"image/jpeg" forhttpheaderfield:@"content-type"]; nsmutabledata *body1 = [nsmutabledata data]; [body1 appenddata:[nsdata datawithdata:data]]; [request1 sethttpbody:body1];  nsdata *returndata; nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding];  nslog(@"ret: %@",returnstring);   nsurlconnection *connreq = [nsurlconnection connectionwithrequest:request1 delegate:self];  if (connreq) {      nslog(@"connection sucessful");     receiveddata = [[nsmutabledata alloc]init];  }  else {      nslog(@"failed");  }  nshttpurlresponse *response = nil; nserror *error = nil; nsdata *respdata = [nsurlconnection sendsynchronousrequest:request1 returningresponse:&response error:&error]; nslog(@"status code: %ld", (long)[response statuscode]); 


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -