将图片附加到AFNetworking 2.0请求的网址? [英] Attach image at URL to AFNetworking 2.0 request?

查看:301
本文介绍了将图片附加到AFNetworking 2.0请求的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



curl -Ffile=@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 constructedBodyWithBlock:^(id< AFMultipartFormData> formData){
NSError * error;
[formData appendPartWithFileURL:[NSURL URLWithString:@http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg] name:@image错误:&错误];
} success:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@Success:%@ *****%@,operation.responseString,responseObject);
}失败:^(AFHTTPRequestOperation * operation,NSError * error){
NSLog(@Error:%@ *****%@,operation.responseString,error);
}];

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



查看使用AFNetworking 2.0上传图片,我尝试使用扩展功能,但仍然得到相同的错误。

  [formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@imagefileName:@photo.jpgmimeType:@image / jpegerror:& 

我也尝试过下载文件,然后使用其他帖子,但我一直得到相同的错误:

  __ 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 constructedBodyWithBlock:^(id< AFMultipartFormData> formData){
[formData appendPartWithFormData:imageData name: 图片];
} success:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@Success:%@ *****%@,operation.responseString,responseObject);
}失败:^(AFHTTPRequestOperation * operation,NSError * error){
NSLog(@Error:%@ *****%@,operation.responseString,error);
}];

以下是完整的错误讯息:

  ***** Error Domain = com.alamofire.error.serialization.response Code = -1011请求失败:错误请求(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=必须重新验证,私人,最大年龄= 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 =请求失败:错误的请求(400)}



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


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请求的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆