我正在尝试将 png 图像和一些参数上传到我的 rails 服务器,我从http://nsscreencast.com/episodes/31-posting-multi-part-forms-with-afnetworking遵循这个示例
我的代码片段如下
当代码运行时,我看到进度从 0% 到 95..98%(看起来它从未完成过)。它从来没有碰到我的 rails 控制器。它也会冻结我的 IOS 应用程序......知道为什么它会冻结我的应用程序吗?我究竟做错了什么?谢谢,欢迎/赞赏任何评论或意见。
NSData *imageData = UIImagePNGRepresentation(image);
NSDictionary *params = @{
@"latte[location]" : @"value1",
@"latte[submitted_by]" : @"value2",
@"latte[comments]" : @"value3"
};
NSURLRequest *putRequest = [[MApiClient sharedInstance] multipartFormRequestWithMethod:@"PUT"
path:@"somepath.json"
parameters:params
constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFileData:imageData
name:@"latte[photo]"
fileName:@"latte.png"
mimeType:@"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:putRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"%f", (totalBytesWritten / (float) totalBytesExpectedToWrite));
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode == 200 || operation.response.statusCode == 201) {
NSLog(@"whats here, %@", responseObject);
} else {
NSLog(@"whats here, %@", responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"whats here, %@", error);
}];
[[MyApiClient sharedInstance] enqueueHTTPRequestOperation:operation];