iphone - Image post to Server with data json not working ios -


i have tried post image data using following code.data getting upload image not uploading.

-(void)imageupload {     name=@"har9233";     userid=@"2969";     cityid=@"1";     mobile=@"9888329329";     mobileverify=@"no ";     gender=@"1";      //nsstring *imagedata=@"12";      nsdata *imagedata = uiimagejpegrepresentation(imageview.image, 1.0);     nsmutabledictionary* _params = [[nsmutabledictionary alloc] init];      [_params setobject:userid forkey:@"userid"];     [_params setobject:name forkey:@"profiledisplayname"];     [_params setobject:gender forkey:@"gender"];     [_params setobject:cityid forkey:@"cityid"];     [_params setobject:mobile forkey:@"mobile"];     [_params setobject:mobileverify forkey:@"ismobileverified"];      nsstring *boundary = @"ghkyre–nhjfhdj-74f5f-gfg5-gggff";      nsstring* fileparamconstant =@"image";      //     nsurl* requesturl = [nsurl urlwithstring:@"myurl"];      nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init];     [request setcachepolicy:nsurlrequestreloadignoringlocalcachedata];     [request sethttpshouldhandlecookies:no];     [request settimeoutinterval:30];     [request sethttpmethod:@"post"];      // set content-type in http header     nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary];     [request setvalue:contenttype forhttpheaderfield: @"content-type"];      // post body     nsmutabledata *body = [nsmutabledata data];      // add params (all params strings)     (nsstring *param in _params) {         [body appenddata:[[nsstring stringwithformat:@"--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]];         [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"\r\n\r\n", param] datausingencoding:nsutf8stringencoding]];         [body appenddata:[[nsstring stringwithformat:@"%@\r\n", [_params objectforkey:param]] datausingencoding:nsutf8stringencoding]];     }  // add image data   if (imagedata)  {         nslog(@"2");         [body appenddata:[[nsstring stringwithformat:@"--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]];         [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"; filename=\"harryimg1.png\"\r\n",fileparamconstant] datausingencoding:nsutf8stringencoding]];        [body appenddata:[@"content-type: image/jpeg\r\n\r\n" datausingencoding:nsutf8stringencoding]];         [body appenddata:[nsdata datawithdata:imagedata]];         [body appenddata:[[nsstring stringwithformat:@"\r\n"] datausingencoding:nsutf8stringencoding]];        }  // [body appenddata:[[nsstring stringwithformat:@"--%@--\r\n", boundary] datausingencoding:nsutf8stringencoding]];   // setting body of post reqeust [request sethttpbody:body];  // set content-length nsstring *postlength = [nsstring stringwithformat:@"%d", [body length]]; [request setvalue:postlength forhttpheaderfield:@"content-length"];  // set url [request seturl:requesturl]; nsurlresponse *response;  nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:nil];  nsdictionary *jsonresponsedata = [nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutablecontainers error:nil]; nslog(@"data=%@",jsonresponsedata);  } 

i had same problems, , have been using code quite yours. try this, works sure, using:

- (nsstring *)randomstringwithlength:(int)length     {          nsstring *letters = @"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789";         nsmutablestring *randomstring = [nsmutablestring stringwithcapacity:length];          (int = 0; < length; i++) {             [randomstring appendformat: @"%c", [letters characteratindex: arc4random() % [letters length]]];         }          return randomstring;     }  -(bool)uploadimagefile:(nsstring *)filename     {         nsmutableurlrequest *request= [[nsmutableurlrequest alloc] init];         [request seturl:[nsurl urlwithstring:@"yoururl"]];          [request sethttpmethod:@"put"];         nsstring *boundary = [self randomstringwithlength:14];         nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary];         [request addvalue:contenttype forhttpheaderfield: @"content-type"];         nsmutabledata *postbody = [nsmutabledata data];         nsdata *data = [nsdata datawithcontentsoffile:filename];         [postbody appenddata:[nsdata datawithdata:data]];         [request sethttpbody:postbody];          nsurlresponse *urlresponse = nil;         nserror *requesterror = nil;         nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse error:&requesterror];          if (((nshttpurlresponse *)urlresponse).statuscode != 201) {             nslog(@"unable upload file: %@, response data: %@, response code: %i, request error: %@", [filename lastpathcomponent], [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding], ((nshttpurlresponse *)urlresponse).statuscode, requesterror);             return no;         }          nslog(@"uploading file successful");         return yes;      } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -