php - Upload multiple images along with other parameters -
how upload array of images along other parameters, images captured device camera , needed uploaded. far i've tried code below :
converting captured image nsdata :
for(int = 0 ; i< imagearray.count ; i++) { uiimage *images = [imagearray objectatindex :i]; nsdata *datas = uiimagepngrepresentation(images); [images addobject:datas]; }
retrieve saved array of images :
nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults]; arrayofimages = [userdefaults objectforkey:@"image"];
uploading images server:
nsdictionary *params =@{ @"name":self->name.text, @"contact_no":self->contactno.text,@"email_id":self->emailid.text,@"s_date":date,@"s_time":time,@"streat":street,@"city":city,@"state":state,@"zip":zipcode}; nsdata *uploaddata = data; nsstring *urlstring = [nsstring stringwithformat:@"url"]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@"post"]; nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; nsstring *knewline = @"\r\n"; nsmutabledata *body = [nsmutabledata data]; (nsstring *name in params.allkeys) { nsdata *values = [[nsstring stringwithformat:@"%@", params[name]] datausingencoding:nsutf8stringencoding]; [body appenddata:[[nsstring stringwithformat:@"--%@%@", boundary, knewline] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"", name] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"%@%@", knewline, knewline] datausingencoding:nsutf8stringencoding]]; [body appenddata:values]; [body appenddata:[knewline datausingencoding:nsutf8stringencoding]]; } for(int = 0; < arrayofimages.count ; i++) { uploaddata = [arrayofimages objectatindex:i]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"file_name[]\""] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-type: application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[nsdata datawithdata:uploaddata]]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n",boundary] datausingencoding:nsutf8stringencoding]]; } [request sethttpbody:body]; [request sethttpbody:body]; [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue currentqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) { nsstring *returnstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"%@",returnstring); }];
php code :
$filescount = count($_files['file_name']['name']); for($i = 0; $i < $filescount; $i++){ $_files['file_name1']['name'] = time().'_'.$_files['file_name']['name'][$i]; $_files['file_name1']['type'] = $_files['file_name']['type'][$i]; $_files['file_name1']['tmp_name'] = $_files['file_name']['tmp_name'][$i]; $_files['file_name1']['error'] = $_files['file_name']['error'][$i]; $_files['file_name1']['size'] = $_files['file_name']['size'][$i]; $config['upload_path'] = $upload_path; $config['allowed_types'] = '*'; $this->load->library('upload'); $this->upload->initialize($config); if($this->upload->do_upload('file_name1')){ $upload_data = $this->upload->data(); $name_array[] = $upload_data['file_name']; $filename = $upload_data['file_name']; $images[] = $filename; }else{ $data['upload_errors'][$i] = $this->upload->display_errors(); } }
afhttprequestoperationmanager *manager = [[afhttprequestoperationmanager alloc] initwithbaseurl:[nsurl urlwithstring:@“your url”]]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; manager.responseserializer = [afjsonresponseserializer serializerwithreadingoptions:nsjsonreadingallowfragments]; manager.responseserializer.acceptablestatuscodes = [nsindexset indexsetwithindex:200]; manager.responseserializer.acceptablecontenttypes = [nsset setwithobjects:@"application/json", @"text/html", nil]; afhttprequestoperation *operation = [manager post:@"url"parameters:_paramaters constructingbodywithblock:^(id<afmultipartformdata> formdata) { [formdata appendpartwithfiledata:imagedata name:@"filename" filename:@"filename.jpg" mimetype:@"image/jpeg"]; } success:^(afhttprequestoperation *operation, id responseobject) { } failure:^(afhttprequestoperation *operation, nserror *error) { }]; [operation start];
you can use afhttprequestoperationmanager instead , code above , worked me.
Comments
Post a Comment