AFnetworking取消特定上传过程

时间:2015-02-25 18:30:27

标签: ios afnetworking-2

我在服务器上上传多张图片我的图片,所有人的网址相同。 以下是代码。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSDictionary *parameters = @{KEY: VALUE};
NSString *imageUrl=[NSString stringWithFormat:@"%@/user/upload/item",BASE_SERVER_ADDRESS];


AFHTTPRequestOperation *op =    [manager POST:imageUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    NSString *imageName = [NSString stringWithFormat:@"IMG00%i.png",count];

    [formData appendPartWithFileData:_imageData name:@"files" fileName:imageName mimeType:@"image/png"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
    //Upload Progress bar here
    NSLog(@"progress updated(percentDone) : %f", percentDone);

}];
[op start];

我并行上传10张图片,并获得每张图片的进度。现在我想取消特定的上传图片,如第5张图片。 如何获得该图像的操作。 operationQueue内部只有NSOperation。我怎么知道具体的NSOperation是我要取消的那个。 谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在变量中存储需要取消的操作,然后调用[op cancel]