【问题标题】:AFNetworking 3.0 upload imageAFNetworking 3.0 上传图片
【发布时间】:2016-07-13 11:58:24
【问题描述】:

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

【问题讨论】:

    标签: ios afnetworking


    【解决方案1】:

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

    -> 这里 kBaseURL 表示服务器的 Base URL

    ->serviceName 表示服务名称。

    ->dictParams 表示需要时给出参数,否则为零。

    ->image 表示传递你的图像。

    注意:- 这段代码是用 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];
    
    }
    

    --> 现在申请到我们的项目中。

    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
        }];
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多