使用AFNetworking issu上载进度

2024-06-03 08:12:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的问题是: 除了上传进度可视化,它工作得很好。太快了。所以我的进度条显示了几毫秒后的完整上传,然后等待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];

Tags: 图像imageself参数tableoperationlongsetvalue