AFNetworking上传文件

-(void)upload{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary *params = @{@"deviceName":@"imageName",@"deviceId":@"1",@"deviceChildId":@"11"};

    [manager POST:@"http://127.0.0.1:8080/upload" parameters:params

                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

         

        UIImage *image = [UIImage imageNamed:@"a.jpg"];

        NSData *imageData = UIImageJPEGRepresentation(image, 1);

         

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        formatter.dateFormat = @"yyyyMMddHHmmss";

        NSString *str = [formatter stringFromDate:[NSDate date]];

        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];

         

        // 上传图片,以文件流的格式

        [formData appendPartWithFileData:imageData name:@"deviceImg"

                                fileName:fileName mimeType:@"image/jpeg"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

         

        NSLog(@"上传成功返回结果 : %@",responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"error::::%@",error);

    }];

}

 

你可能感兴趣的:(NetWork)