2

情况:我正在使用 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];
4

0 回答 0