更新
我认为问题出在networkQueue上。当我用[request startAsynchronous]或[request startSynchronous]替换[self.networkQueue addOperation:request]时,它可以工作。
我更新了代码以使其更清晰。
原来的
我使用 ASIHTTPRequest 上传带有编码图像(或 [request setFile:imagefile...])的 json 文件,但我无法更新进度。
我只有一个输出:value: 1.000000,这意味着上传完成。
incrementUploadSizeBy从未被触发。
我在网上搜索了很多,但仍然找不到答案。这是我的代码。
+ (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);
}