问题描述
我想使用AFNetworking 2.0在iOS中执行此CURL请求:
c> c> c / cURL的 code> appendPartWithFileData 函数中的code>参数。如果我把它改为 name 它工作原理:
[formData appendPartWithFileData: imageData name:@filefileName:@image.jpgmimeType:@image / jpeg];
但似乎我必须使用该方法。如果我尝试添加网址,它会继续失败:
[formData appendPartWithFileURL:[NSURL URLWithString:@... ] name:@filefileName:@image.jpgmimeType:@image / jpeg错误:错误]
,即使我使用文件URL
的真实文件名
I am trying to perform this CURL request in iOS using AFNetworking 2.0:
curl -F "file=@picture.jpg" "https://image.groupme.com/pictures?access_token=token123"
using this image: http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg
Looking at iOS Image upload via AFNetworking 2.0
I tried:
[self POST:[NSString stringWithFormat:@"pictures?access_token=%@", access_token] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSError *error; [formData appendPartWithFileURL:[NSURL URLWithString:@"http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg"] name:@"image" error:&error]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@ ***** %@", operation.responseString, error); }];
but I kept getting the error: {"errors":["http: no such file"]}
Looking at Uploading image with AFNetworking 2.0, I tried using the extended function, but kept getting the same error.
[formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg" error:&error];
I also tried downloading the file first and then uploading it with the exact code used in both of those other posts, but I kept getting the same error:
__block NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg"]]; NSDictionary *parameters = @{@"image":@"YES"}; [self POST:[NSString stringWithFormat:@"pictures?access_token=%@", access_token] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFormData:imageData name:@"image"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@ ***** %@", operation.responseString, error); }];
Here is the full error message:
***** Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x14fa92dc0> { URL: https://image.groupme.com/pictures?access_token=XXXX } { status code: 400, headers { "Access-Control-Allow-Credentials" = false; "Access-Control-Allow-Headers" = "Accept, Content-Type, X-Requested-With, X-Access-Token, User-Agent, Pragma, Referrer, Cache-Control, Origin"; "Access-Control-Allow-Methods" = "POST, GET, PUT, DELETE, OPTIONS"; "Access-Control-Allow-Origin" = "*"; "Access-Control-Max-Age" = 86400; "Cache-Control" = "must-revalidate, private, max-age=0"; Connection = "keep-alive"; "Content-Length" = 34; "Content-Type" = "application/json;charset=utf-8"; Date = "Tue, 19 Apr 2016 09:50:42 GMT"; Server = "nginx/1.8.1"; "X-Gm-Service" = "image-service"; } }, NSErrorFailingURLKey=https://image.groupme.com/pictures?access_token=XXXXX, com.alamofire.serialization.response.error.data=<7b226572 726f7273 223a5b22 68747470 3a206e6f 20737563 68206669 6c65225d 7d0a>, NSLocalizedDescription=Request failed: bad request (400)}
It is the same error if I download the image then attach it, or if I just attach it with the URL.
How can I attach an image to a form request in AFNetworking?
Looks like the file part of the cURL correlated with the name parameter in the appendPartWithFileData function. If I change it to name it works:
[formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg"];
But it seems I have to use that method. If I try just add the URL, it continues to fail:
[formData appendPartWithFileURL:[NSURL URLWithString:@"..."] name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg" error:&error]
even if I use the real filename from the file URL
这篇关于将图片附加到AFNetworking 2.0请求的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!