0

update

I think the problem is with the networkQueue. When I replace [self.networkQueue addOperation:request] with [request startAsynchronous] or [request startSynchronous], it works.

I update the code to make it more clear.

original

I use ASIHTTPRequest to upload a file of json with encoded image (or [request setFile:imagefile...]), but I cannot update the progress.

I got only one output: value: 1.000000, which means the uploads finished.

incrementUploadSizeBy was never triggered.

I have searched online a lot but still cannot find a answer. Here is my code.

+ (ASIEngine *)sharedInstance {
    static ASIEngine *sharedInstance = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [[self alloc] init];
    });

    return sharedInstance;
}

- (id)init {
    if (self = [super init]) {
        _networkQueue = [ASINetworkQueue queue];
        [_networkQueue setMaxConcurrentOperationCount:MAX_CONCURRENT_OPERATION_COUNT];
        [_networkQueue setDelegate:self];
        [_networkQueue go];
    }
    return self;
}

    - (void)upload:(NSString *)imageJsonString onCompletion:(void(^)(NSString *responseString))onCompletion onFailed:(void(^)(NSError *error))onFailed {

        __unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPLOAD_URL]];
        [request setShowAccurateProgress:YES];
        [request setUploadProgressDelegate:self];

        [request setPostValue:imageJsonString forKey:@"imageString"];

        [request setCompletionBlock:^{
            NSString *responseString = [request responseString];
            onCompletion(responseString);
        }];

        [request setFailedBlock:^{
            onFailed([request error]);
        }];

        [self.networkQueue addOperation:request];
    }

    #pragma mark - ASIProgressDelegate

    - (void)setProgress:(float)newProgress {
        NSLog(@"value: %f", newProgress);
    }

    - (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength {
        NSLog(@"data length: %lld", newLength);
    }
ThinkChris
  • 1,169
  • 2
  • 15
  • 38

1 Answers1

1

If you are uploading a string, you probably have so little data to upload (as opposed to when uploading a picture), that it uploads everything in one step. You could try reading a huge string from a file and see what happens.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • Hi thanks for the response. The string's from a image, about 1m. I tried setFile:imageUrl, addData:imageData, all the same. I have updated the question, please have a look. – ThinkChris Aug 15 '12 at 10:10