I am facing issues in uploading media files to a web service built in php.
I have an app along with source code in android and I am suppose to replicate it in iphone.
There is this part where I am supposed to upload media files along with some strings to a web service.
The code in android uses a class called MultiPartEntity and HTTPPost to upload it.
There are several tags , and the tags are associated with some strings.
for ex.MultiPartEntity mpEntity.addPart(“username”, “abc”);, similarly for password and other tags.
I made some research and I found ASIFormDataRequest which deals with similar things.
here is the sample code I am using to upload media along with some other detials,
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@”http://my.url.com/somephpwebservice.php”%5D%5D;
[request setHTTPMethod:@”POST”];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// now lets create the body of the post
NSMutableData *body = [NSMutableData data];
//................................ Append the image data here//u= \"TP\";p= \"7012\";o= \"DEMO\";j= \"1151\"...........................
//if (!([htmlCodeToWrite rangeOfString:@"SignatureShouldBeDoneHere"].location == NSNotFound))
//{
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;name=\"userfile\";username=\"TP\";p=\"7012\";filename=\"%@\"\r\n",@"superman.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
//}
//NSString *content = [NSString stringWithUTF8String:[imageData bytes]];
NSLog(@"%@",imageData);
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//data into the string..... verifying............
NSString *strTest = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
NSLog(@"%@ %@",@".....the request for webService",strTest);
// setting the body of the post to the request
[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,@".....the response from webService");
Is this the proper procedure.??
The php developer says the image is not getting uploaded.
Please help.
Thanx in advance.
Please refer to my response in this post. I think is the correct way to conform a multipart upload file request. Cheers!