I want to send file to server path with a paramater "filepath" as one parameter, and file data as another paramater. How do I do it.Here in the following I am appending filepath with data. but I guess it is wrong please help
NSURL *nsurl =[NSURL URLWithString:_urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setURL:nsurl];
[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];
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"Model.png"], 0.0);
NSString *string = [NSString stringWithFormat:@"filepath=%@",_filePath];
NSData *pathData = [string dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:pathData];
//Image
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",@"newFile.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\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:kNilOptions error:nil];
NSLog(@"%@",dict);
thanks
You can't just do
[body appendData:pathData];
at the start. You need to add it with the appropriate boundary and content information. So you should have a number of lines like:And repeat from 2 for each additional piece of data to be added.
Check the spec for information on the appropriate content types and disposition info that needs to be added for each data type (perfect example for you is at the bottom).