post request changing image name iOS objective c - ios

how to change the name of the image??Actually I want to append the phone number from user at image name to make a unique name for image
below is my action button code
NSString *filename =[NSString stringWithFormat:#"%#cache",self.tmpp1];
NSData *imageData = UIImageJPEGRepresentation(_profilei.image, 1.0);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"unique-consistent-string";
// 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 are strings
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#\r\n\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", #"image"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#; filename=imageName.jpg\r\n", #"imageFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// parameter username
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"customer_name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"%#",_nametf.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
the phone number is store in the string mobilestrp and I want to append coz my server accept is if it has the number.Thanks in advance for any suggestions!!

Related

Image didn't place in server side using multipart?

In my code i am trying to add string and images in server side but strings are getting placed image didn't get stored in to server...
if (internetStatus != NotReachable)
{
//set content type for http request
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:localRegister]];
[request setHTTPMethod:#"POST"];
NSMutableData *body=[NSMutableData data];
NSString *boundary=#"123456789";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
//adding first name
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"content-Disposition:form-data;name=\"firstname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtFirstName.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding email id
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"content-Disposition:form-data;name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtEmailID.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding telephone number
NSInteger mobilenumber;
mobilenumber=[[NSString stringWithFormat:#"%#",txtMobileNo.text] integerValue];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"content-Disposition:form-data;name=\"telephone\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtMobileNo.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//adding password
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"content-Disposition:form-data;name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtPassWord.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if(!imgdata)
{
NSLog(#"entering in to image side");
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"content-Disposition:form-data;name=\"pic\"\r\n\r\n filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgdata]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
Here i am getting a image to upload server side..
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
_signupImgView.image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
imageSending=[UIImage imageWithData:[NSData dataWithContentsOfURL:path]];
imgdata = UIImagePNGRepresentation(imageSending);
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
i know some thing wrong in my program please help me to find out my problem.

Multipart/form-data with multiple header and multiple content, is not generated after appending imagedata in iOS

NSString *urlString = #"http://example.com/avatar";
NSLog(#"%#",urlString);
NSString *imageName = #"hello";
NSString *string = [NSString stringWithFormat:#"%#%#%#", #"Content-Disposition: form-data; name=\"avatar\"; filename=\"", imageName, #".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449MJ";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data"];
[request addValue:contentType forHTTPHeaderField: #"Content-type"];
NSString *contentType1 = [NSString stringWithFormat:#"%#",customer_api_key];
[request addValue:contentType1 forHTTPHeaderField:#"Authorization"];
NSString *contentType2 = [NSString stringWithFormat:#"app-customer"];
[request addValue:contentType2 forHTTPHeaderField:#"Requester"];
UIImage *image=DPView.image;
NSData *imageData =UIImageJPEGRepresentation(image, 1);
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: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_mime_type\"\r\n\r\nimage/jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"customer_id\"\r\n\r\n%d",customer_id] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(#"Body = %#",body);
NSString *bodydecoded = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
NSLog(#"Bodydecoded = %#",bodydecoded);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"ReturnData = %#",returnData);
if (returnData) {
NSString *s11= [[NSString alloc] initWithData:returnData encoding:NSISOLatin1StringEncoding];
// NSDictionary *responseDictionary1= (NSDictionary *)s11;
NSLog(#"S11 = %#",s11);
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [s11 dataUsingEncoding:NSASCIIStringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
NSLog(#"json finally= %#",JSON);
hello everyone, I am new to IOS so please spare me.
Here I am trying to send Image with 3 header for authorization and 3 contents in body for image related info. But as I append the imagedata and then decode the body, I get this output :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
ÿØÿà
rest of the content after image is not created or what?
but when do not append the imagedata, i get the whole form-data created normally as :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_name"
fgh
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_mime_type"
image/jpg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="customer_id"
xyz
-----------------------------14737809831466499882746641449MJ
and when i pass this data(in both cases), i get invalid data error from server.(obviously)
please help me, I am stuck.
also the headers I am passing is without boundary = %#,boundary, is that correct. I removed it because I was getting invalid api key error from server.(off-course the authorization error)
Thanks in advance.
please use this for creating multipart form requset
NSString *boundary = [NSString stringWithFormat:#"Boundary-%#", [[NSUUID UUID] UUIDString]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
don't add header in body add as follows
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
create body ad follows
NSMutableData *body = [NSMutableData data];
[dicParams enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image\"; filename=\"%#\"\r\n", strName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", strMime] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:dataImage];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
request is created and use as you want
===================
when you are adding parameters you are doing like following
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
"\r\n" is the end tag form which one field is completed.
your first parameter will start with #"\r\n--%#\r\n" which is wrong. this means one field ended and another field is started instead of that you have to do as mentioned below.
add boundary like this
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
add field name like this
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
add field value like this
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
you will get success.

Upload files (images, pdf) with parameters to the server

I would like to upload 1pdf file and array of images.
I can upload 1pdf and 2jpg files, if jpg files more than two I get message: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x174268000 {NSDebugDescription=No value.}
Why is this happening? Also I tested server with POSTMAN and it work well.
NSURL *URL = [NSURL URLWithString:constFileUploadURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"0xLhTaLbOkNdArZ";
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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *userid = [NSString stringWithFormat:#"%li",userID];
[body appendData:[userid dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[constBackendKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSData *data in arrayWithFiles)
{
NSString *fileName;
if ([arrayWithFiles indexOfObject:data] == 0)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
fileName = #"Content-Disposition: form-data; name=\"files\"; filename=\"PdfFile.pdf\"\r\n";
[body appendData:[fileName dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"files%ld\"; filename=\"image%ld.jpg\"\r\n",[arrayWithFiles indexOfObject:data],[arrayWithFiles indexOfObject:data]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
_connection = [NSURLConnection connectionWithRequest:request delegate:self];
Cod works, problem was on server side. File uploading 50mb, a post size 8mb.

post array of images to server from iOS

I want to upload array of images from iPhone to server,how should I append array to NSMutableData.
Here is my code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://companyname.com/projectname/webservice.php?"]];
NSString *boundary = #"---------------------------239413274531762";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
NSMutableData *body = [NSMutableData data];
[request setHTTPMethod:#"POST"];
[request addValue:#"Keep-Alive" forHTTPHeaderField:#"Connection"];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
//TASK ---- companydetails
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"task\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"uploadmanuals" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter1 --- userid
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:strUserID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter1 --- stockdict
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"productname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:productname] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//Upload company logo image ......complogo
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:
#"Content-Disposition: form-data; name=\"uploadmanual\"; filename=\"%#\"\r\nContent-Type: image/jpeg\r\n\r\n",
#"logo.jpg"]dataUsingEncoding:NSUTF8StringEncoding]];
UIImage * imageToUpload = generatePhotoThumbnail(self.imgCompanyLogo.image);
if(imageToUpload) {
[body appendData:UIImageJPEGRepresentation(imageToUpload,1.0f)];
}
else {
[body appendData:UIImageJPEGRepresentation(self.imgCompanyLogo.image,0.5f)];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
I am able to send single image using this now I want to send array of images.
please help.

iPhone SDK : Multiple images upload to server with SOAP request

I know very well that you may count this thread as duplicate just because multiple times this question was asked in different ways. I did not find any solution from the existing questions on this forum and thus i'm going to make a new thread.
Here, I've a code for uploading multiple images at a time. So, please note down the sentence that is "multiple images upload".
I want to upload 1 or 2 or 3 or 4 images along with some text parameters and the issue that i'm facing is "same image is uploaded multiple times" mean single image having multiple times on server.
Example :
NSString *urlString = [NSString stringWithFormat:#"%#/MultipleImageUpload",soapAction];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[theRequest setHTTPShouldHandleCookies:NO];
[theRequest setTimeoutInterval:60];
[theRequest setURL:[NSURL URLWithString:urlString]];
[theRequest setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",BOUNDARY_MULTIPART_IMAGE];
[theRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
//parameter SecurityCode
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"SecurityCode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[SEC_CODE dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter LoggedInUserID
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"LoggedInUserID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#“23433” dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter Device
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Device\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#“iPhone” dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
for (int i=0; i<[arrImgs count]; i++) {
UIImage* image = [arrImgs objectAtIndex:i];
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"product_photo\"; filename=\"%d.jpg\"\r\n", i] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[theRequest setHTTPBody:body];
return theRequest;
Please let me know if anyone has the solution for this one ..
Thanks,
Nilesh M. Prajapati
Sorry everyone,
But i got the solution. Actually, I have to set dynamic filename in below lines.. I have to set "product_photo" as a dynamic string having random number like "product_photo1" or "product_photo2", etc..
for (int i=0; i<[arrImgs count]; i++)
{
UIImage* image = [arrImgs objectAtIndex:i];
NSData *imgData = UIImagePNGRepresentation(image);
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BOUNDARY_MULTIPART_IMAGE] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"product_photo%d\"; filename=\"%d.png\"\r\n", i,i] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}

Resources