2013-03-25 72 views
0

我有一个ZIP文件上传到服务器时,我使用此代码:的iOS:上传ZIP文件与AFNetworking

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" 
+0

http://stackoverflow.com/questions/8234186/afnetworking-uploading-a-file – iPatel 2013-03-25 14:56:32

回答

1

你不应该关闭句柄到拉链文件在尝试附加到请求之前?否则,您无法确定它是否完全写入存储,因此您可能会遇到此问题。

[zip1 CloseZipFile2]; 
+0

就是这样,谢谢。 – 2013-03-25 16:08:45