zoukankan      html  css  js  c++  java
  • PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现

    PHP 上传文件接口:

    //保存图片
    $json_result ['status'] = 0;
    $path = 'upfile';
    $json_result ['status'] = 0;
    $json_result ['successmsg'] = '上传失败';
    if (isset ( $_FILES ['image'] )) {
        $upfile = 'upfile/' . $_FILES ['image'] ['name'];
        if (! @file_exists ( $path )) {
            @mkdir ( $path );
        }
        $result = @move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $upfile );
        if (! $result) {
            $json_result ['status'] = 0;
            $json_result ['successmsg'] = '上传失败';
            $json_result ['datas'] = array ('savePath' => $upfile );
            exit ( json_encode ( $json_result ) );
        }
    }
    
    
    $json_result ['status'] = 1;
    $json_result ['datas'] = array ('savePath' => "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/".$upfile );
    print_r(json_encode($json_result));

    iOS通过接口上传图片:

    // 上传头像
    - (void)uploadUserHeadImage:(UIImage *)image {
        // 获得网络管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        // 设置请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        
        [manager POST:@"http://localhost:8888/upload_image.php" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            // 获取图片数据
            NSData *fileData = UIImageJPEGRepresentation(image, 1.0);
            
            // 设置上传图片的名字
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
            
            [formData appendPartWithFileData:fileData name:@"image" fileName:fileName mimeType:@"image/png"];
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            NSLog(@"%@", uploadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 返回结果
            NSLog(@"%@", responseObject[@"datas"]);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    }
  • 相关阅读:
    Spyder汉化问题
    Python配置环境变量
    Python、Spyder的环境搭建
    自然语言处理(六)词向量
    自然语言处理(五)深度学习
    自然语言处理(三)主题模型
    自然语言处理(四)统计机器翻译SMT
    自然语言处理(二) 语言模型
    自然语言处理(一)基础知识
    机器学习(九)隐马尔可夫模型HMM
  • 原文地址:https://www.cnblogs.com/mafeng/p/5868558.html
Copyright © 2011-2022 走看看