Я пытался загрузить изображение на свой сервер AWS, используя данные многостраничной формы afnetworking 3.0, но, по-видимому, каждое изображение, которое я пытался загрузить, попало в блок сбоя. Коды записаны в objective-c.
- (NSURLSessionUploadTask *)uploadImageTask:(NSMutableDictionary *)dictionary
{
NSString *s3BucketUrl = [dictionary objectForKey:@"s3BucketUrl"];
NSData *imageData = [dictionary objectForKey:@"imageData"];
NSString *contentType = [dictionary objectForKey:@"contentType"];
NSString *filename = [dictionary objectForKey:@"filename"];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:s3BucketUrl parameters:dictionary constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:filename mimeType:contentType];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask = [[NSURLSessionUploadTask alloc] init];
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(onUploadImageTask:)])
{
[self.delegate onUploadImageTask:nil];
}
} else {
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(onUploadImageTask:)])
{
[self.delegate onUploadImageTask:error];
}
}
}];
[uploadTask resume];
return uploadTask;
}
Я получаю сообщение об ошибке
Домен ошибки = com.alamofire.error.serialization.response Code = -1011 «Ошибка запроса: неверный запрос (400)» UserInfo = {NSUnderlyingError = 0x170248940 {Error Domain = com.alamofire.error.serialization.response Code = -1016 » Ошибка запроса: недопустимый тип содержимого: application / xml "UserInfo = {com.alamofire.serialization.response.error.response = {URL: https://....s3.amazonaws.com/} {код состояния: 400, заголовки {Connection = close; «Content-Type» = «приложение / xml»; Date = "Вт, 20 декабря 2016 г., 01:53:47 GMT"; Сервер = AmazonS3; «Transfer-Encoding» = Идентичность; "x-amz-id-2" = "....."; "x-amz-request-id" = ....; }},
Я попытался изменить тип содержимого для сериализатора ответа, но все равно не смог.