使用AFNetworking问题上传进度

时间:2013-04-18 21:51:44

标签: python ios afnetworking

情况: 我正在使用AFNetworking通过我的iOS应用程序的http-post请求上传一些数据到我的网络服务器。特别是它的一些参数和图像。

我的问题: 除上传进度可视化外,它工作正常。这太快了。所以我的进度条在几毫秒之后显示完整的上传,而不是它等待3-4秒,直到它完成。

它看起来只是测量参数字典上传而不是图像上传。它还能取决于服务器发回的内容吗?我正在运行python ...

NSData *imageData = UIImageJPEGRepresentation(image, 0.6f);

NSMutableDictionary* paramsDictionary = [NSMutableDictionary dictionaryWithDictionary:[table dictRespresentation]];
[paramsDictionary setValue:[NetConnectionManager generateRequestID:table] forKey:@"table"];
[paramsDictionary setValue:someString forKey:@"string"];

NSMutableURLRequest *request = [self.httpClient multipartFormRequestWithMethod:@"POST" path:@"/table" parameters:paramsDictionary constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"picture" fileName:@"picture.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    self.uploadProgress = (100.0f / totalBytesExpectedToWrite) * totalBytesWritten;
    NSLog(@"Sent %lld of %lld bytes   progress: %f", totalBytesWritten, totalBytesExpectedToWrite, self.uploadProgress);

}];

[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"success: %@", operation.responseString);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@",  operation.responseString);

}
];

[self.httpClient enqueueHTTPRequestOperation:operation];

0 个答案:

没有答案