I need to know how a file to be uploaded on server as multipart/form-data; POST request using objective C & COCOA
Asked
Active
Viewed 160 times
1 Answers
0
I just tried this and this works for me. I have uploaded an image and passed few required paramters to the API. NSMutableURLRequest * theURLrequest =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:UPLOAD_FILE_URL]]; [theURLrequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------FOO";
// Setting Content Type to multipart
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[theURLrequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
// setting another parameter like any file attribute
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"param1_name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@\r\n", valueOfParam1ToBeSentOnserver] dataUsingEncoding:NSUTF8StringEncoding]];
// setting another parameter like any file attribute
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"param2_name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%d\r\n", valueOfParam2ToBeSentOnserver] dataUsingEncoding:NSUTF8StringEncoding]];
//////////////////
// setting up data bytes of file to be sent and file name
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", documentName] dataUsingEncoding:NSUTF8StringEncoding]];
// Setting type of contents being sent
NSString *contentTypeMime = @"image/jpeg";
[postbody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n",contentTypeMime] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:pDataToUpload]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];;
[postbody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/////////////////////////////////////
[theURLrequest setValue:[NSString stringWithFormat:@"%d",[postbody length]] forHTTPHeaderField:@"Content-Length"];
[theURLrequest setHTTPBody:postbody];
//Adding basic authentication in the request
NSString* authString=[NSString stringWithFormat:@"%@:%@",pUserName,pPassword];
NSData* authData=[authString dataUsingEncoding:NSASCIIStringEncoding];
NSString* authValue=[NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[theURLrequest addValue:authValue forHTTPHeaderField:@"Authorization"];
NSURLConnection *temp=[[NSURLConnection alloc] initWithRequest:theURLrequest delegate:self];
self.mConnection=temp;
[theURLrequest release];
[temp release];

Saturn
- 151
- 1
- 7