我正在尝试使用AFNetworking 2.0将图像上传到Parse.com。 当我下载存储在'Parse'上的图像时,生成的文件已损坏。
通过做差异,我看到以下数据已经附加到开头:
文件末尾还附加了'boundry'数据。
我做错了什么?哑剧型?
我使用'curl'发送相同的文件,一切正常。
这是我用来发送的代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
[manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[manager.requestSerializer setValue:value forHTTPHeaderField:key];
}];
[manager POST:@"https://api.parse.com/1/files/imagefile.jpg"
parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
NSData *imgData = UIImageJPEGRepresentation(image, 1.0f);
[formData appendPartWithFileData:imgData
name:@"imagefile"
fileName:@"imagefile.jpg"
mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation, error);
}
];
更新
以下代码按预期工作(不是多部分,因此显然这是有问题的区域):
NSMutableString *urlString = [NSMutableString string];
[urlString appendString:@"https://api.parse.com/1/"];
[urlString appendFormat:@"files/imagefile.jpg"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"***********" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request addValue:@"***********" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[request addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:UIImageJPEGRepresentation(image, 0.3f)];
NSURLResponse *response = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSString *fileUrl = [httpResponse allHeaderFields][@"Location"];