我必须将 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"
最佳答案
在尝试将 zip 文件附加到请求之前,您是否应该关闭该文件的句柄?否则,您无法确定它是否已完全写入存储,因此您可能会遇到此行为。
[zip1 CloseZipFile2];
关于iOS:使用 AFNetworking 上传 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15618011/