0

我正在尝试使用 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)}

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

4

1 回答 1

0

将您的代码更改为我的 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,所以不重要,它只是给出默认值。

希望它有帮助

于 2016-06-16T10:21:21.223 回答