js实现拖拽上传

helei 2020-1-3 755 1/3
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/1.12.0/jquery.js"></script>
    <style>
        #drop_area {
            position: relative;
            width: 300px;
            height: 150px;
            border: 1px dashed #ddd;
            border-radius: 5px;
            margin-bottom: 5px;
        }
        #drop_area .text {
            position: absolute;
            width: 100%;
            top: 60px;
            text-align: center;
            font-size: 14px;
        }
        #img_area {
            width: 300px;
        }
        #img_area img {
            width: 300px;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div id="drop_area">
        <div class="text"><span>+</span><span>将文件拖到此处,即可上传</span></div>
    </div>
    <div id="img_area"></div>
</body>

<script>
    var dp = document.getElementById('drop_area');
    dp.addEventListener('dragover', function(e) {
        e.stopPropagation();
        //阻止浏览器默认打开文件的操作
        e.preventDefault();
        e.dataTransfer.dropEffect = 'copy';
    });
    
    //单图上传
    // dp.addEventListener("drop", function(e) {
    //     e.stopPropagation();
    //     //阻止浏览器默认打开文件的操作
    //     e.preventDefault();
    //     var files = e.dataTransfer.files;
    //     var file = files[0];
    //     var formData = new FormData();
    //     formData.append("file", file);
    //     $.ajax({
    //         type: 'post',
    //         url: '/upload',
    //         data: formData,
    //         contentType: false, //必须  禁止jQuery设置Content-Type请求头
    //         processData: false, //必须  禁止jQuery处理发送的数据
    //         dataType: "json",
    //         success: function(res){
    //             if(res.success == 1){
    //             }
    //         },
    //     });
    // });

    //多图上传
    dp.addEventListener("drop", function(e) {
        e.stopPropagation();
        //阻止浏览器默认打开文件的操作
        e.preventDefault();
        var files = e.dataTransfer.files;
		console.log(files[0]);
        var formData = new FormData();
        for(var i =0; i<files.length; i++){
            formData.append("file"+i, files[i]);
        }
        $.ajax({
            type: 'post',
            url: '/upload',
            data: formData,
            contentType: false, //必须  禁止jQuery设置Content-Type请求头
            processData: false, //必须  禁止jQuery处理发送的数据
            dataType: "json",
            success: function(res){
                if(res.success == 1){
                    res.realPathList.forEach(function(item){
                        $('#img_area').append('<img src="'+item+'">');
                    });
                }
            },
        });
    });
</script>
</html>
- THE END -

helei

1月03日16:32

最后修改:2020年1月3日
0

非特殊说明,本博所有文章均为博主原创。

共有 0 条评论