我必须将 ZIP 文件上传到服务器,我正在使用以下代码:
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:methode parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"data.xml"];
ZipArchive *zip1 = [[ZipArchive alloc] init];
BOOL ret = [zip1 CreateZipFile2:[documentsDirectory stringByAppendingPathComponent:@"data.zip"]];
ret = [zip1 addFileToZip:databasePath newname:@"data.xml"];
[formData appendPartWithFileData:[NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"data.zip"]]
name:@"FILE"
fileName:@"data.zip"
mimeType:@"application/zip"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation start];
ZIP 文件很好。请求很好。但服务器响应显示文件在传输途中已损坏。服务器只接收 855 字节的 ZIP 文件,而不是 931 字节。
服务器管理员告诉我(没有 iOS 经验)我必须设置正确的上传类型(就像在 HTML 中一样):
enctype="multipart/form-data"