美文网首页
iOS客户端上传图片&&Thinkphp Upl

iOS客户端上传图片&&Thinkphp Upl

作者: 慢慢来111 | 来源:发表于2017-12-27 15:56 被阅读18次
    多图上传
    NSString *mnPath = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
        NSString *cyPath = [[NSBundle mainBundle] pathForResource:@"草原.jpg" ofType:nil];
        UIImage *mnImage = [[UIImage alloc] initWithContentsOfFile:mnPath];
        UIImage *cyImage = [[UIImage alloc] initWithContentsOfFile:cyPath];
        NSData *mnData = UIImageJPEGRepresentation(mnImage, 0.7f);
        NSData *cyData = UIImageJPEGRepresentation(cyImage, 0.7f);
        NSArray *dataArray = @[mnData,cyData];
        
        //发起网络请求
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        [manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
            // 上传多张图片
            for(NSInteger i = 0; i < dataArray.count; i++)
            {
                // 取出单张图片二进制数据
                NSData * imageData = dataArray[i];
                
                // 图片数据对应的key值,没有这个key 服务器$_FILES 获取不到数据流
                NSString * Name = [NSString stringWithFormat:@"%ld", i+1];
                
                // 图片名
                NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
                [formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
            }
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    
    

    单图上传

        NSString *path = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
        UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:path];
        NSData *data = UIImageJPEGRepresentation(savedImage, 0.7f);
        
        // 发起网络请求
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        [manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            // name :图片数据对应的key值,没有这个key 服务器$_FILES 获取不到数据流
            
            [formData appendPartWithFileData:data name:@"file" fileName:@"WE.jpg" mimeType:@"image/jpeg"];
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    

    服务端

         /**
         * 多图/单图上传
         */
    
        public function upload_goods(){
    
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 4000000 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './uploads/goods_img/'; // 设置附件上传根目录
            $upload->savePath = ''; // 设置附件上传(子)目录
            $upload->autoSub = true;
            // 上传文件
            $info = $upload->upload($_FILES);
            if(!$info) {
                $this->ajaxReturn(apiData($upload->getError(), true));
            }else{
                $this->ajaxReturn(apiData($info, true));
            }
        }
    

    返回数据

    backData.png

    相关文章

      网友评论

          本文标题:iOS客户端上传图片&&Thinkphp Upl

          本文链接:https://www.haomeiwen.com/subject/oxpugxtx.html