How to cancel multiparts uploading file inbetween ios(如何取消后台线程)
How to cancel multiparts uploading file inbetween ios(how to cancel background thread)
你好,我正在使用此代码以多部分格式将图像上传到服务器。以及所有工作文件。但是根据我的要求,如果图像数据很重(大),那么我想取消中间的上传。
我的密码是
NSString *imagePostUrl = [NSString stringWithFormat:@"%@/users/%@/profilepic",Kwebservices,[[NSUserDefaults standardUserDefaults] valueForKey:@"l_userid"]];
//
NSString *fileName = [NSString stringWithFormat:@"profilePic%ld%c%c.png", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSDictionary *parameters = @{@"":@""};
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
manager.securityPolicy.allowInvalidCertificates = YES;
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
AFHTTPRequestOperation *op = [manager POST:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imagedata name:fileName fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
NSURL *imageURL = [NSURL URLWithString:imagestr1];
imageViewProfile.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
[kappDelegate HideIndicator];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
[HelperAlert alertWithOneBtn:AlertTitle description:@"Failed to upload selected profile picture" okBtn:OkButtonTitle];
[kappDelegate HideIndicator];
return ;
}];
[op start];
如何在中途停止上传。
[(AFHTTPRequestOperationManager*)manager.operationQueue cancelAllOperations];
您可以保留对操作的引用并调用NSOperation
方法cancel
。
有关如何处理此问题的更多信息:
步骤:
- 声明属性:
@property (strong, nonatomic) AFHTTPRequestOperation *imagePostOperation;
- 将代码中的属性声明替换为
self.imagePostOperation = [manager POST:imagePostUrl ...
- 当你想取消操作时:
[self.imagePostOperation cancel]
- 在操作 succes/failure 回调块中,检查
[operation isCancelled]
以管理该案例。
你好,我正在使用此代码以多部分格式将图像上传到服务器。以及所有工作文件。但是根据我的要求,如果图像数据很重(大),那么我想取消中间的上传。
我的密码是
NSString *imagePostUrl = [NSString stringWithFormat:@"%@/users/%@/profilepic",Kwebservices,[[NSUserDefaults standardUserDefaults] valueForKey:@"l_userid"]];
//
NSString *fileName = [NSString stringWithFormat:@"profilePic%ld%c%c.png", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSDictionary *parameters = @{@"":@""};
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
manager.securityPolicy.allowInvalidCertificates = YES;
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
AFHTTPRequestOperation *op = [manager POST:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imagedata name:fileName fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
NSURL *imageURL = [NSURL URLWithString:imagestr1];
imageViewProfile.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
[kappDelegate HideIndicator];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
[HelperAlert alertWithOneBtn:AlertTitle description:@"Failed to upload selected profile picture" okBtn:OkButtonTitle];
[kappDelegate HideIndicator];
return ;
}];
[op start];
如何在中途停止上传。
[(AFHTTPRequestOperationManager*)manager.operationQueue cancelAllOperations];
您可以保留对操作的引用并调用NSOperation
方法cancel
。
有关如何处理此问题的更多信息:
步骤:
- 声明属性:
@property (strong, nonatomic) AFHTTPRequestOperation *imagePostOperation;
- 将代码中的属性声明替换为
self.imagePostOperation = [manager POST:imagePostUrl ...
- 当你想取消操作时:
[self.imagePostOperation cancel]
- 在操作 succes/failure 回调块中,检查
[operation isCancelled]
以管理该案例。