15年软件开发经验 只做源码定制 互联网+定制化解决方案

15年软件开发经验,只做源码定制!

原创设计 定制开发

满足您的个性化需求

当前位置:首页 后端开发 Thinkphp5

layui,上传文件,auto:false //选择文件后不自动上传

教腾豪| 发布于 2021-11-06 18:01:17| 247阅读| 0点赞| 0评论
举报

点击上传不自动上传到服务器,做一下上传前的回调,

参考文档  https://layui.11dz.cn/doc/modules/upload.html


上传文件,点击不自动上传到服务器,做上传前的回调,获取到文件name

这里还需要用到一个

bindAction: '#test2_t' //指向一个按钮触发上传

bindAction属性是一个触发上传的按钮

上传前的回调choose

 ,choose: function(obj){
                //将每次选择的文件追加到文件队列
                var files = obj.pushFile();
                //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9)
                obj.preview(function(index, file, result){
                    // console.log(index); //得到文件索引
                    // console.log(file); //得到文件对象
                    // console.log(result); //得到文件base64编码,比如图片
                    var html ='
">10px;">'+file.name+'" id="cha" src="/wp-content/themes/dt-the7-child03/images/cha.png" alt="">" name="file" value="'+result+'">
'
$("#inpuss_er").append(html); }); }

// console.log(index); //得到文件索引 // console.log(file); //得到文件对象 // console.log(result); //得到文件base64编码,比如图片

这个是上传前回调,返回的数据,如果是多文件,layui会进行遍历


以下是代码部分


<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        var ming = $('#firstname').val();
        var xing = $('#lastname').val();
        var email = $('#email').val();

        //点击多图上传的X,删除当前的图片
        $("body").on("click",".inpusss_er",function(){
            $(this).parent(".inpuss_er").remove();
        });
        var uploadInst = upload.render({
            elem: '#test2'
            ,url: 'http:/wp-content/themes/dt-the7-child03/php/upload1.php'
            ,accept:"file"
            ,data:{ming:ming,xing:xing,email:email}
            ,auto: false //选择文件后不自动上传
            ,bindAction: '#test2_t' //指向一个按钮触发上传
            ,choose: function(obj){
                //将每次选择的文件追加到文件队列
                var files = obj.pushFile();
                //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9)
                obj.preview(function(index, file, result){
                    // console.log(index); //得到文件索引
                    // console.log(file); //得到文件对象
                    // console.log(result); //得到文件base64编码,比如图片
                    var html ='
'+file.name+''">
'
$("#inpuss_er").append(html); }); } ,done: function(){ document.getElementById('light').style.display='block'; document.getElementById('fade').style.display='block'; $("#inpuss_er").empty(); } ,error: function(){ layer.msg('上传失败'); } }); }); $('#queren').click(function(){ document.getElementById('light').style.display='none'; document.getElementById('fade').style.display='none'; window.location.reload(); })
script>


下面是上传文件的入口代码

"Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
header('Access-Control-Allow-Headers:x-requested-with,content-type');

if(isset($_FILES))
{
    $curlPost = $_POST['ming'].' '.$_POST['xing'].'_'.$_POST['email'];
    $www=$_FILES['file'];
    $scr = $www['tmp_name'];
    $ext = substr($www['name'],strrpos($www['name'],'.')+1); // 上传文件后缀
    $path = $www;
    $fp = fopen($scr, 'rb');
    $size = filesize($scr);
    // $path1=['path'=>"/450 7th avenue's shared workspace/tax panda/Tax Act Client List/Test/1761594081@qq.com/Client Uploaded/".date('YmdHis').$www['name'].'.'.$ext];
    $path1=['path'=>"/TaxAct Client List/A-2022 Client Doc/$curlPost/Client Uploaded/".$www['name']];
    $cheaders = array('Authorization: Bearer WEOR0m62p_QAAAAAAAAAAaE2ScoPo8uB5q-MvMsUUuGkEZ1N7bELP65BRgH0pLo3',
        'Content-Type: application/octet-stream',
        'Dropbox-API-Arg: '.json_encode($path1));
    $ch = curl_init('https://content.dropboxapi.com/2/files/upload');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    print_r($response);
    curl_close($ch);
    fclose($fp);
    return $response;
//    if(isset(json_decode($response,true)['error_summary']))
//    {
//        echo json_encode(['code'=>1]);
//    }else{
//        echo json_encode(['code'=>2]);
//    }

}
?>
0

0条评论

别默默看啦~登录/注册一起参与讨论吧~

热门标签

教腾豪
微信扫一扫立即咨询
账号登录|扫码登录

立即注册 |忘记密码?

欢迎注册

已有账号马上登录

重置密码

扫码绑定微信
微信扫一扫

绑定手机号

分享到-微信

举报

  • 举报类型:

  • 举报描述:

您好,当前积分不足。

在线客服
拨打电话
17330196230 13230981129
顶部