AFNetworking 3.0上传图片

时间:2016-07-13 11:58:25

标签: ios afnetworking

我尝试使用AFNetworking 3.0将图像上传到我的服务器。我的服务器返回“请选择要上传的文件”。以下是我在php文件$filename = $_FILES["file"]["name"];中捕获上传文件的方法。

-(void)uplodeImages :(NSString *)image {
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://local/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:image] name:@"file" fileName:@"imagename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                          NSLog(@"Laddar...");
                      });
                  } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      if (error) {
                          NSLog(@"Error: %@", error);
                      } else {
                          NSLog(@"%@ %@", response, responseObject);
                      }
                  }];
    [uploadTask resume];
}

1 个答案:

答案 0 :(得分:3)

注意: - 我刚刚使用AFNetworking 3.0实现了图片上传服务,

- &GT;这里kBaseURL表示服务器的基本URL

- &gt; serviceName表示服务名称。

- &gt; dictParams表示根据需要提供参数,否则为nil。

- &gt;图片意味着传递您的图片。

注意: - 此代码用NSObject类编写,我们已在项目中使用。

+ (void)requestPostUrlWithImage: (NSString *)serviceName parameters:(NSDictionary *)dictParams image:(UIImage *)image success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {

NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[SVProgressHUD show];

NSData *fileData = image?UIImageJPEGRepresentation(image, 0.5):nil;

NSError *error;
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:strService parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    if(fileData){
        [formData appendPartWithFileData:fileData
                                    name:@"image"
                                fileName:@"img.jpeg"
                                mimeType:@"multipart/form-data"];
    }
} error:&error];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

    NSLog(@"Wrote %f", uploadProgress.fractionCompleted);
}
                                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                      [SVProgressHUD dismiss];
                                      if (error)
                                      {
                                          failure(error);
                                      }
                                      else
                                      {
                                          NSLog(@"POST Response  : %@",responseObject);
                                          success(responseObject);

                                      }
                                  }];

[uploadTask resume];

}

- &GT;现在申请我们的项目。

UIImage *imgProfile = //Pass your image.        


    [WebService requestPostUrlWithImage:@"save-family-member" parameters:nil image:imgProfile success:^(NSDictionary *responce) {

        NSString  *check = [NSString stringWithFormat:@"%@",[responce objectForKey:@"status"]];
        if ([check  isEqualToString: @"1"]) {
           //Image Uploaded. 
        }
        else
        {
         //Failed to Upload.
        }


    } failure:^(NSError *error) {
        //Error
    }];