views:

194

answers:

0

Hi, I am trying to upload multipart data to my server which has image data along with the fields specified in the following form

<form action="/imageUploader" enctype="multipart/form-data" method="post">
<p>
Title:<br>
<input type="text" name="title" size="30">
<input name="action" type="hidden" value="uploadtile" />
<input name="canvasid" type="hidden" value="44" />
</p>
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="fileupload" size="44">
</p>

<input type="submit" value="Send">
</form>


NSString *urlString = @"http://localHost:8080/imageUploader";
//NSString *mainurlString =  [NSString stringWithFormat:@"%@?jsession=%@",urlString,jsession];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"AaB03x"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];


NSData *imageData = UIImageJPEGRepresentation(mSelectedImage,.5);
NSMutableData *body = [NSMutableData data];
NSData *separator = [[NSString stringWithFormat:@"\r\n-----------%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:separator];


forTitlePart = [NSString stringWithFormat:@"Content-Disposition:form-data;action=uploadtile"];
[body appendData:[forTitlePart dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:separator];


forTitlePart = [NSString stringWithFormat:@"Content-Disposition:form-data;name=\"canvasid\"2"];
[body appendData:[forTitlePart dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:separator];

forTitlePart = [NSString stringWithFormat:@"Content-Disposition: file; name=\"title.JPG\""];
[body appendData:[forTitlePart dataUsingEncoding:NSUTF8StringEncoding]];
forTitlePart = [NSString stringWithFormat:@"Content-Type: image/JPG "];
[body appendData:[forTitlePart dataUsingEncoding:NSUTF8StringEncoding]];
forTitlePart = [NSString stringWithFormat:@"Content-Transfer-Encoding: binary"];
[body appendData:[forTitlePart dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:separator];

[request setHTTPBody:body];

NSURLResponse *urlResponse;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                           returningResponse:&urlResponse error:nil];

However I am not able to upload the image, I have also checked with the server but it is not receiving any request data. Any help will be greatly appreciated.