2011-11-07 63 views
2

如何檢查AFNetworking是否已完成文件的上傳?我該如何檢查是否有問題?AFNetworking如何檢查上傳操作是否已完成或失敗

這是我的代碼:

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server"]]; 
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 
[parameters setObject:[fieldName text] forKey:@"name"]; 
[parameters setObject:[fieldSurname text] forKey:@"surname"]; 

NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithFileData:[[AttachedFile sharedAttachedFile]dataToSend] mimeType:@"image/jpeg" name:@"attach"]; 
}]; 

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease]; 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 

queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue addOperation:operation]; 

,我應該實現方法(和委託)?

謝謝!使用

回答

3

[AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest 
               success:(void (^)(id object))success 
               failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure]; 

代替

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease]; 

當你的文件完全上傳成功調用塊。如果有問題,則調用故障塊。

0

HTTPRequestOperationWithRequest已被棄用,你最好使用setCompletionBlockWithSuccess:在AFNetworking Uploading a file

上AFHTTPRequestOperation實例失敗

檢查mattt的答案