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.
Related
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!!
I am using this code and able to send only single image to the server. I
want to send multiple selected image files to the server.
Please look this code for possible mistake and Please help me
out to solve this issue. Thank you for your time.
-(void)submitDataToServer
{
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSNumber *randomNo = [NSNumber numberWithInteger: timeStamp];
NSLog(#"Random No=%#",randomNo);
NSLog(#"Project ID=%#",projectId);
NSLog(#"DATE=%#",dateString);
NSLog(#"USER ID=%#",userID);
NSLog(#"COMMENT =%#",projectNoteTextView.text);
NSLog(#"TASK ID=%#",task_Id);
//-- Convert string into URL
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:UPLOADDATA_URL]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
//-- Append data into posr url using following method
NSMutableData *body = [NSMutableData data];
//Random No
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"random"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",randomNo] dataUsingEncoding:NSUTF8StringEncoding]];
//Project ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"project_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",projectId] dataUsingEncoding:NSUTF8StringEncoding]];
//Date
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"date"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",dateString] dataUsingEncoding:NSUTF8StringEncoding]];
//User ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"user_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",userID] dataUsingEncoding:NSUTF8StringEncoding]];
//Comment
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"comment"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",projectNoteTextView.text] dataUsingEncoding:NSUTF8StringEncoding]];
//Task ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"task_assign_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",task_Id] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"ARRAYCOUNT: %lu", (unsigned long)arrImage.count);
for (int y = 0; y < [arrImage count]; y++)
{
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * image_name =[NSString stringWithFormat:#"%#%d",#"image",y];
NSLog(#"IMAGE NAME:%#", image_name); //self.arrImage
UIImage* imageFile = [arrImage objectAtIndex:y];
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[imageFile drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *dataImg = [NSData
dataWithData:UIImagePNGRepresentation(newImage)];
//NSData *dataImg = [NSData dataWithData:UIImagePNGRepresentation([arrImage objectAtIndex:y])];
NSLog(#"added %i", y+1);
NSLog(#"Images, send to server:%#", dataImg);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"site_image\"; filename=\"%#\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//-- Sending data into server through URL
[request setHTTPBody:body];
//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"Result = %#",result);
}
Use this code in for loop for multiple images.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:#"imgProfile"];
NSString *str=[NSString stringWithFormat:#"%#",yourUrl];
NSString *urlString = [NSString stringWithFormat:#"%#",str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"profilePicture\"; filename=\"ved.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter image
if (imageData.length<1) {
[body appendData:[#" " dataUsingEncoding:NSUTF8StringEncoding]];
}
else {
[body appendData:[NSData dataWithData:imageData]];
}
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter Method
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"profileUpdate" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter 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 stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"userid"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//userId , email , name , tockenId , deviceType , cCode,profilePicture,phone
//parameter email
[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:[txtEmail.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter name
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",txtName.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter tockenId
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"tockenId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"Device_Token"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Parameter deviceType
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"deviceType\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",#"iOS"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter cCode
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"cCode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtCountryCode.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// //parameter profilePicture
// [body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"profilePicture\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[#"ved" dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter phone
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"phone\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtPhone.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSLog(#"Update profile Image %#",dict);
dispatch_async(dispatch_get_main_queue(), ^{
if ([[dict valueForKey:#"result"] isEqualToString:#"sucess"]) {
[WebServiceHelper showAlertViewForTitle:#"Message" andMessage:[dict valueForKey:#"message"]];
}
else{
[WebServiceHelper showAlertViewForTitle:#"Updating Failed!" andMessage:[dict valueForKey:#"message"]];
}
});
});
}
After recheck I got that there was I was passing wrong image Array. Now this is working after changed the same. Thanks you all for your time and consideration.
NSLog(#"ARRAYCOUNT: %lu", (unsigned long)imgArray.count);
for (int y = 0; y < [arrImage count]; y++)
{
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * image_name =[NSString stringWithFormat:#"%#%d",#"image",y];
NSLog(#"IMAGE NAME:%#", image_name);
UIImage* imageFile = [arrImage objectAtIndex:y];
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[imageFile drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *dataImg = [NSData
dataWithData:UIImagePNGRepresentation(newImage)];
//NSData *dataImg = [NSData dataWithData:UIImagePNGRepresentation([imgArray objectAtIndex:y])];
NSLog(#"added %i", y+1);
NSLog(#"Images, send to server:%#", dataImg);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"site_image\"; filename=\"%#\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
Blockquote
}
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.
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]];
}
Over the past week I have been trying to get this to work. What I am trying to do is upload an image to an S3 service as part of the pretty hacky way that this API that I am working with uses to upload images. The API is documented here: http://www.thingiverse.com/developers/upload-guide . I am having problems with step 2. They want you do upload to S3, then tell their other servers that the file is uploaded to S3, rather than doing it for you. Regardless, this is the code that I am using to upload the image:
+ (NSString *)uploadPictureWithParameters:(NSDictionary *)parameters toURL:(NSString *)actionURL withImage:(UIImage *)sourceImage {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
NSString *boundaryConstant = #"-------------------------acebdf13572468";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundaryConstant];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSLog(#"%#", [parameters allKeys]);
if ([parameters objectForKey:#"AWSAccessKeyId"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"AWSAccessKeyId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"AWSAccessKeyId"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"bucket"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"bucket\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"bucket"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"key"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"key"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"acl"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"acl"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"success_action_redirect"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"success_action_redirect\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"success_action_redirect"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"policy"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"policy"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"signature"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"signature"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"Content-Type"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"Content-Type"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"Content-Disposition"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Content-Disposition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-disposition: attachment; filename=photo.png\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// This would put them in the wrong order:
/*for (NSString *param in [parameters allKeys]) {
[body appendData:[[NSString stringWithFormat:#"---------------------------%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}*/
NSLog(#"%d", body.length);
UIImage *resizedImage = [sourceImage resizedImageToFitInSize:CGSizeMake(700, 700) scaleIfSmaller:NO];
NSData *imageData = UIImagePNGRepresentation(resizedImage);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"photo.png\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"Request Body: %#", requestBody);
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%d", body.length);
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"Request Body After Image: %#", requestBody);
[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSString * urlString = [NSString stringWithFormat:#"%#", actionURL];
NSLog(#"%#", urlString);
NSURL *aUrl = [NSURL URLWithString: urlString];
[request setURL:aUrl];
NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &error];
NSLog(#"%p, %#", error, error.localizedDescription);
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
return responseString;
}
The error that I am getting from that is this:
Invalid according to Policy: Policy Condition failed: ["eq", "$Content-Disposition", ""]
I've been researching this online and it seems that the errors produced by S3 are typically not of much relevance to the error. So, can anyone tell me what I am doing incorrectly here?
Thanks