Here I need to upload the mp3 audio by using the post method, but wrote the code for uploading the audio but i dont know how include the parameters.
kindly check my code and help me to attach parameters.
NSString *baseurl = UploadAudio;
// baseurl = [NSString stringWithFormat:#"%#user_id=%#&comments=%#&login_key=%#",baseurl,[[sharedvar userdict] objectForKey:#"user_id"],[self.audiodict objectForKey:#"comments"],[[sharedvar userdict] objectForKey:#"login_key"]];
NSLog(#"baseurl :%#",baseurl);
NSURL *dataURL = [NSURL URLWithString:baseurl];
NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[dataRqst setHTTPMethod:#"POST"];
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",stringBoundary];
[dataRqst addValue:headerBoundary forHTTPHeaderField:#"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// -------------------- ---- Audio Upload Status ---------------------------\\
//pass MediaType file
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; userfile=\"file upload\"; filename=\"uploadaudio.mp3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"Content-Type: audio/mp3\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//*******************load locally store audio file********************//
NSString *audioUrl = [[self audiodict] objectForKey:#"audiopath"];
NSLog(#"audioUrl :%#",audioUrl);
// get the audio data from main bundle directly into NSData object
NSData *audioData;
audioData = [[NSData alloc] initWithContentsOfFile:audioUrl];
// add it to body
[postBody appendData:audioData];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:#"--%#--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [postBody appendData:postData];
// add body to post
[dataRqst setHTTPBody:postBody];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:dataRqst delegate:self];
[connection start]; // connection
My parameters are userid and session.Could any body help me for adding these parameters and values
You can use ASIHTTPRequest for sending Audiofiles and additional parameters. It is vary easy to use. Just 2 line of code .It helped me a lot :-)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setFile:#"YOUR FILE PATH WITH FILE EXTENSION" forKey:#"YOUR FILE KEY"];
Just before the line // final boundary add these lines:
//Append userid
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"%#\r\n", myUserID] dataUsingEncoding:NSUTF8StringEncoding]];
use these 3 lines over and over to append more fields e.g. the session field.
Related
NSString *filenames = [NSString stringWithFormat:#"front_img"];
NSString *urlString = #"http://ccccc.com/upfile.php";
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"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"filenames1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames1 dataUsingEncoding:NSUTF8StringEncoding]];
/*Sending First Image start here*/
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n", filenames]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData1]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/*Sending First Image ends here*/
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
A typical upload http body will look like:
--BOUNDARY_TEXT
Content-Type/Content-Disposition info key values
Base64 encoded file data ...
--BOUNDARY_TEXT
Content-Type/Content-Disposition info key values
Base64 encode file data.
--BOUNDARY_TEXT
You've done the first one, just continue append files according to the format seperatored by the BOUNDARY.
I am developing an iOS app and I am using node.js for server side scripting. I am facing problem in uploading image to server from iOS app. It is working fine if I am uploading image from webpage form. But if uploading from app side it is not working.
//test file
h3 Pic Upload
form(action='/pic_upload', method='post',enctype='multipart/form-data')
| user_pic:
input(type='file', name='user_pic')
input(type='submit')
//app.js
var userlogin =require('./routes/userlogin');
app.post('/pic_upload', userlogin.picUpload);
//userlogin.js
//picUpload function
exports.picUpload = function(req, res) {
console.log(req.files); // showing undefined, when called from IOS app
// pic upload script...
});
I have tried sending image from app side as data or file parameter but it did not work. How to send the file parameter from app side so that I can easily upload the image to server? Kindly suggest a way to tackle the problem.
This code works in my app. Try this.
NSData *imgData = UIImageJPEGRepresentation(newImg, 0.2);
NSString *str = #"displayImage";
NSString *urlString = #"http://some.url.com/post";
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithFormat:#"---------------------------14737809831464368775746641449"];
// 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--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imgData) {
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"displayImage\"; filename=\"image.jpg\"\r\n"]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set URL
[request setURL:[NSURL URLWithString:urlString]];
NSString *bodyStr = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#" %#",bodyStr);
NSURLConnection *lot_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(lot_Connection)
{
webdata = [[NSMutableData alloc] init];
}
I have a simple question- I'm currently writing a specific part in my app related to sending data to server.
I tried to send text, succeeded. Sent image, succeeded.
What I'm trying to do now is to send them both within one POST request.
I figured out that I need to use something that is called multipart/form-data and boundaries, but I haven't found anymore info about it.
So how can I send both text and image in one, simple POST request? And how can I check for errors during upload, afterwards etc.?
Thanks!
Reference code I've written but sending 0 bytes of info:
NSData *imageData = UIImageJPEGRepresentation(sendImage, 1.0);
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://posttestserver.com/post.php?dir=something"]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------54737809831466490885746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"rn--%#rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"userfile\"; filename=\"reportingImage.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"rn--%#--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: text/xml" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
This is a working snippet that sends text and image, optionally a few texts with a few params per each
//After dismissing the alert, we get its text (user location and notes) and the picture he took
NSMutableData *body = [NSMutableData data];
NSURL *url = [NSURL URLWithString:#"http://posttestserver.com/post.php?dir=Doda"]; //Test server, you can access it online to see the upload
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449"; //I have no idea what this is, but without it the code won't work
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[req setValue:contentType forHTTPHeaderField: #"Content-Type"];
//Attaching image
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: attachment; name=\"imageOfReport\"; filename=\"imageOfReport.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(sendImage, 1.0)]]; //Crucial, getting a JPEG version of the image and sending it
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"report_description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]]; //Crucial, taking the text from the Alert and sending it
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];
//Below are few lines which can add other parameters and text
/* [body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"spid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];*/
NSURLConnection *sendingTheData2 = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; //Sent! ;)
You probably want to use a network library like AFNetworking, and save yourself some time :)
How can I create a post with an image and attach it as a featured image from iPhone app?
I am using wordpress with JSON API plugin
My url to create post: <-- Reference to this
http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals
Now I need to attach an image from the phone gallery to this url. How can I do this?
Try this. Use JSON POST method. You can append all your parameters like this.
-(void)uploadImage
{
NSString *method = #"";
NSString *username = #"";
NSString *token = #"";
NSString *urlString = [NSString stringWithFormat:#"%#",#"yoururl"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
// file
NSData *imageData = UIImageJPEGRepresentation( imageView.image, 0.8);
NSString *boundary = [NSString stringWithString:#"---------------------------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:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter username
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter token
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\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:[method dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\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];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
[returnString release];
}
Hope this may help you.
To full fill your requirement you need to send image file on web-service using by POST method
First chose image from Photo Library or take it from the camera and make data of that Image using following code.
NSString *imgName = [NSString stringWithFormat:#"ImageTest"];
NSData *imgData=UIImagePNGRepresentation(imgView.image);
NSString* urlstring = [NSString stringWithFormat:#"http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals&imageFile=%#",imgName];
NSLog(#"URL >> %#",urlstring);
NSURL url = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//===============================================
NSMutableURLRequest postRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[postRequest setURL:url];
[postRequest setHTTPMethod:#"POST"];
NSString boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[postRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [[NSMutableData alloc] init];
[postRequest addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
imgName this is very important object in this code it's image which you need to pass in your web service which is shown as above.
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.png\"\r\n",imgName]] dataUsingEncoding:NSUTF8StringEncoding]]; //img name
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Add Image imgData is Declare as Above.
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:body];
NSURLResponse *response;
NSError error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
NSString *result=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString * jsonRes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(#"result = %#",[result JSONValue]);
It perfectly working with me ! I hope you may get help from here!
in my app i want to upload an image chosen via UIImagePickerController to a database which only accepts JPEG images. I've browsed many questions in here, and in other forums but i still didn't get it to work. I hope you can check my code, if there is a mistake which i can't see.
This is the complete method for uploading, with an image description, an image title, and the geodata for this image:
- (void) uploadPic{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destinationFilePath = [[NSString alloc] initWithFormat: #"%#/%#.jpeg", documentsDirectory, self.chosenImage.imgTitle];
NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath];
NSLog(#"will upload file: %#", destinationFilePath);
NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL]; (*1)
NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90); (*2)
//Here i tried 2 ways of getting the data for uploading, but both don't work.
// create the URL
NSURL *postURL = [NSURL URLWithString:#"http://*********/PictureUpload"];
// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
// change type to POST (default is GET)
[postRequest setHTTPMethod:#"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",
sessionID];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:#"Content-Type"];
// create data
NSMutableData *postBody = [NSMutableData data];
// title part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgTitle dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// desc part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.imgDescription dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// latitude part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.latitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// longitude part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[self.chosenImage.longitude dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"%#.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageURLdata]];
[postBody appendData:[#"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:#"--%#--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
NSLog(#"%#", s);
// add body to post
[postRequest setHTTPBody:postBody];
// pointers to some necessary objects
NSURLResponse* response;
NSError* error;
// synchronous filling of data from HTTP POST response
NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if (error)
{
NSLog(#"Error: %#", [error localizedDescription]);
}
// convert data into string
NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
length:[responseData length]
encoding:NSUTF8StringEncoding];
// see if we get a welcome result
NSLog(#"%#", responseString);
[self responseHandler:responseString];
}
The GEOimage chosenImage is created via the CGImageRef after an UIImage is chosen in the ImagePickerController.
Method nr. *1 to get the NSData for upload is not the best, because here the image is chosen from the document directory, and here every EXIF information about the image is deleted.
With both methods i get the response from the database that the image filetype is not supported (JPEG expected).
I thought with method nr. *2 i'm sending an JPEG image, but perhaps i have a mistake in the whole multipart/formdata process.
I tried to get the URL to the original file on the filesystem, but this is quite difficult for me. I only got the assets-url from the image.
Thanks in advance
S4lfish
You've probably solved it on your own by now, but I think I see your problem.
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",
sessionID];
When you're defining the boundary inside the MIME header, you're using your sessionID as the boundary. Then, down below, you're using your stringBoundary variable to create the actual boundaries.
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
It might also be a good idea to provide the Content-Length of each MIME block to help the processing code, but I don't think that's at issue here.
// Try this ---->>>
NSMutableDictionary *post_dic=[[NSMutableDictionary
alloc]initWithCapacity:20]; [post_dic setObject:firstName_txtFld.text
forKey:#"firstname"]; [post_dic setObject:lastName_txtFld.text
forKey:#"lastname"]; [post_dic setObject:email_txtFld.text
forKey:#"email"]; [post_dic setObject:password_txtFld.text
forKey:#"password"]; [post_dic setObject:country_txtFld.text
forKey:#"address"]; [post_dic setObject:state_txtFld.text
forKey:#"state"]; [post_dic setObject:city_txtFld.text
forKey:#"city"]; [post_dic setObject:zip_txtFld.text forKey:#"zip"];
[post_dic setObject:phoneNumber_txtFld.text forKey:#"phonenumber"];
NSURL *url = [NSURL URLWithString:#"Enter your url"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest
requestWithURL:url]; [urlRequest setHTTPMethod:#"POST"]; [urlRequest
setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *boundary = #"--------------------------14737809831466499882746641449"; NSString
*contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[urlRequest addValue:contentType
forHTTPHeaderField: #"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
for (NSString *param in post_dic) {
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data;
name=\"%#\"\r\n\r\n", param]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"%#\r\n", [post_dic objectForKey:param]]
dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data;
name=\"filedata\";
filename=\"%#.jpg\"\r\n",#"text"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
//NSLog(#" image -->%#",imageData);
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postbody]; [urlRequest setURL:url];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[connection start];