我正在尝试使用POST
请求将多个文件上传到服务器,但由于某种原因,只提交了一个文件。我怀疑我在边界上做错了什么,但我不确定在哪里......
我的代码有什么问题?
[_serverRequest setHTTPMethod:@"POST"];
NSString *_boundary = @"14737809831466499882746641449";
NSString *_contentType = [NSString stringWithFormat:@"multipart/form-data; charset=UTF-8; boundary=%@",_boundary];
[_serverRequest setValue:_contentType forHTTPHeaderField:@"Content-Type"];
/* reqeuest body */
NSMutableData *_requestBody = [NSMutableData data];
for (id _instance in self.currentBrowserInstances)
{
if ([_instance respondsToSelector:@selector(pathToDatabase)])
{
NSString *_databasePath = [_instance pathToDatabase];
NSMutableString *_filename = [NSMutableString stringWithString:[self _generatedFilename]];
[_filename appendFormat:@"_%@", [[_instance name] stringByReplacingOccurrencesOfString:@" " withString:@""]];
if (_databasePath.pathExtension.length > 0)
[_filename appendFormat:@".%@", _databasePath.pathExtension];
/* Build Request Body */
[_requestBody appendData:[[NSString stringWithFormat:@"--%@\r\n", _boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[_requestBody appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; name=\"databases\"; filename=\"%@\"\r\n", _filename] dataUsingEncoding:NSUTF8StringEncoding]];
[_requestBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[_requestBody appendData:[NSData dataWithContentsOfFile:_databasePath]];
[_requestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[_requestBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", _boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[_serverRequest setHTTPBody:_requestBody];
这是服务器响应:
Array
(
[databases] => Array
(
[name] => QTSKUFJM_test.dat
[type] => application/octet-stream
[tmp_name] => /private/var/tmp/php3w7qd5
[error] => 0
[size] => 64576
)
)