PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] How to cancel multiparts uploading file inbetween ios(how to cancel background thread)

 July 31, 2022     image-uploading, ios, multipartform-data, multithreading, objective-c     No comments   

Issue

hello i am using this code to upload an image to server by multiparts format. and everything working file. But according to my requirement if the image data is heavy(large) then i want to cancel the uploading in between.

My Code is

 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];

how can i stop uploading process in middle.


Solution

You can keep a reference to the operation and call the NSOperation method cancel.

For more information on how to handle this:

  • Cancel method reference

  • Responding to the cancel command

Steps:

  1. Declare the property: @property (strong, nonatomic) AFHTTPRequestOperation *imagePostOperation;
  2. Replace the declaration of your protperty inside your code with self.imagePostOperation = [manager POST:imagePostUrl ...
  3. When you want to cancel the operation: [self.imagePostOperation cancel]
  4. In the operation succes/failure callback blocks, check [operation isCancelled] to manage that case.


Answered By - lluisgh
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing