美文网首页
文件的上传和接收代码

文件的上传和接收代码

作者: 鸿雁长飞光不度 | 来源:发表于2018-01-08 01:22 被阅读0次

    1.二进制上传

    1. 1 php接收

     public function upload()
     {
            // 获取表单上传文件
            $file = request()->file('file');
            // 移动到上传临时目录
            $info = $file->rule('uniqid')->move(get_image_path(config('image_folder_temp')));
            if ($info) {
                return $this->returnOk(array('msg' => '上传成功', 'file_name' => $info->getSaveName()));
            } else {
                return $this->returnErr('上传失败,请重试!');
            }
        }
    

    1.2 iOS上传

    NSString *URL = [NSString stringWithFormat:@"%@%@",[HSApi ShareApi].HS_URL_HEAD,@"file/upload"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];//设置服务器允许的请求格式内容
    [manager POST:URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:files name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //[HSCoverView showMessage:@"上传成功"];
        success(JSON[@"file_name"]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    

    1.3 前端canvas画布生成图片上传

    canvas.toBlob(function (blob) {
    // 图片ajax上传
        var fd = new FormData();
        fd.append('file', blob);
        $.ajax({
            type: 'POST',
            url: 'http://appdev.hskaoyan.com/xxx/file/upload',
            data: fd,
            processData: false,
            contentType: false
        }).done(function (data) {
            console.log(data);
        });
    }, 'image/jpeg');
    

    2. base64 加密

    2.1 php接收

      public function saveBaseImage(Request $request)
      {
            if ($request->isPost()) {
                $base64_img = trim($request->param('image'));
                if (!preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
                    // 匹配图片文件失败
                    return $this->returnErr('不支持的文件格式,请重试!');
                }
                $img_ext = $result[2];
                if (!in_array($img_ext, array('jpeg', 'jpg', 'png'))) {
                    // 文件类型错误,不支持该类型
                    return $this->returnErr('仅支持jpg、jpeg、png的图片!');
                }
                // 保存临时图片
                $src_name = config('image_folder_temp') . uniqid() . '.' . $img_ext;
                $src_path = get_image_path($src_name, '', get_server_name());
                if (!file_put_contents($src_path, base64_decode(str_replace($result[1], '', $base64_img)))) {
                    return $this->returnErr('上传头像失败,请重试!');
                }
                return $this->returnOk($src_name);
            }
        }
    

    2.2 canvas画布转图片上传

    var dataURL = canvas.toDataURL("image/png");
    $.ajax({
        async: false,
        url: '/admin/image/saveBaseImage',
        data: {image: dataURL, img_folder: '/tmp/'},
        type: "post",
        success: function (data) {
            alert(data);
        }
    })
    

    相关文章

      网友评论

          本文标题:文件的上传和接收代码

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