我正在尝试将图片从iPhone上传到 Django Web服务,并且我不断收到 400 这条特定消息:
error: {"image": ["No file was submitted. Check the encoding type on the form."]}
。
我已经坐了几个小时试图解决这个问题,我相信不知何故,mime-type没有正确发送。
您是否有任何想法或更好的方式上传图片,而不是我的代码:
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://MYADDRESS"]];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"pass"];
NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSDictionary *parameters = @{@"image" : imageData, @"name" : @"MyImage" , @"Content-Type" : @"image/jpeg"};
AFHTTPRequestOperation *op = [manager POST:@"/api/photo/" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"MyImage" fileName:@"MyImage.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"%f%% Uploaded", round(((float)totalBytesWritten / (float)totalBytesExpectedToWrite) * 100.));
}];
[op start];