无法使用AFNetworking 3.0上传图像

时间:2016-06-16 10:03:12

标签: ios iphone afnetworking afnetworking-3

我正在尝试使用AFHTTPSessionManager上传多个文件(图像),但请求失败,Code = -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)}

如果我做错了,请告诉我。谢谢

1 个答案:

答案 0 :(得分:0)

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

{{1}}

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

希望它有用