2013-03-25 92 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