【问题标题】:Attach image at URL to AFNetworking 2.0 request?将 URL 上的图像附加到 AFNetworking 2.0 请求?
【发布时间】:2016-08-11 11:02:56
【问题描述】:

我正在尝试使用 AFNetworking 2.0 在 iOS 中执行此 CURL 请求:

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

使用这张图片:http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg

看着iOS Image upload via AFNetworking 2.0

我试过了:

[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);
}];

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

查看Uploading image with AFNetworking 2.0,我尝试使用扩展功能,但一直收到同样的错误。

[formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg" error:&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);
}];

这是完整的错误信息:

***** 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 中将图像附加到表单请求?

【问题讨论】:

  • 使用[formData appendPartWithFileData:name:fileName:mimeType:],而不是[formData appendPartWithFormData:name:]

标签: ios curl ios9 afnetworking-2 groupme


【解决方案1】:

看起来 cURL 的 file 部分与 appendPartWithFileData 函数中的 name 参数相关。如果我将其更改为 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 中的真实文件名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多