2

我需要知道如何将文件作为 multipart/form-data 上传到服务器;使用 Objective C & COCOA 的 POST 请求

4

1 回答 1

0

我刚试过这个,这对我有用。我已经上传了一张图片并将一些必需的参数传递给了 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];
于 2013-04-26T08:52:50.760 回答