【问题标题】:Unable to upload images with AFNetworking 3.0无法使用 AFNetworking 3.0 上传图像
【发布时间】:2016-06-16 10:03:11
【问题描述】:

我正在尝试使用 AFHTTPSessionManager 上传多个文件(图像),但请求失败,代码=-1011“请求失败:内部服务器错误 (500)”。

这是我的代码。

NSString *urlString = [BaseUrl stringByAppendingString:UploadImages];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *apiKey = [[Utility sharedInstance] getObjectForKey:API_KEY];
[manager.requestSerializer setValue:apiKey forHTTPHeaderField:@"Authorization"];

[manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData){

    for(int i = 0 ;i < pictures.count; i++){
        UIImage *image = [pictures objectAtIndex:i];
        NSData * imageData = UIImageJPEGRepresentation(image, 0.5);
        [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i] fileName:[NSString stringWithFormat:@"file%d.jpg",i] mimeType:@"image/jpeg"];
    }

}progress:nil success:^(NSURLSessionTask *task, id responseObject){
    NSLog(@"Pictures Uploaded");
}failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Uploading failed %@",[error localizedDescription]);
}];

失败:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x14803b080> { URL: https://BaseUrl/v1/imgupload } { status code: 500, headers {
Connection = close;
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Thu, 16 Jun 2016 09:25:43 GMT";
Server = "Apache/2.4.7 (Ubuntu)";
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.14";
 } }, NSErrorFailingURLKey=https://BaseUrl/v1/imgupload, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: internal server error (500)}

如果我做错了什么请告诉我。谢谢

【问题讨论】:

  • 由于错误表明它很可能是服务器错误而不是您的代码问题,如果有疑问,请使用邮递员
  • 是的,内部服务器错误 500 是在服务器出现问题时引起的,例如服务器关闭或不可用时
  • 服务器没有关闭。在邮递员中工作正常,但在 AFNetworking 中不行。
  • 我认为它同样的失败告诉我,然后发送 png 图像然后它的成功,

标签: ios iphone afnetworking afnetworking-3


【解决方案1】:

将您的代码更改为我的 for 循环而不是您的 for 循环,

for(int i = 0 ;i < pictures.count; i++){
        UIImage *imag = [pictures objectAtIndex:i];
        NSData * imageData = UIImagePNGRepresentation (imag);
        [formData appendPartWithFileData:imageData name:@"Photo" fileName:@"Image.png" mimeType:@"image/png"];
    }

名称,文件名不重要,它存储的图像有id,所以它不重要,它只是给出默认值。

希望对你有帮助

【讨论】:

  • 是的,这很重要。使用 for 循环只会上传一张图片。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
相关资源
最近更新 更多