FormData 拖拽上傳 顯示上傳進(jìn)度

upload接口地址

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>拖拽</title>
    <style>
        #drag_box{
            width:100px;
            height:100px;
            border:1px solid orange;
            background:#ccc;
            cursor: pointer;
        }
        #box{
            width:100%;
            height:400px;
            background:#eee;
            margin:10px 0px;
        }
        #box .item{
            float:left;
            margin:5px;
            position:relative;
        }
        #box .item .item-progress{
            position:absolute;
            left:50%;
            top:50%;
            width:80px;
            height:80px;
            transform: translate(-50%, -50%);
            background:rgba(0,0,0,.6);
            border-radius:40px;
            color:#fff;
            text-align: center;
            line-height:80px;
        }
        #box img{
            
            max-height: 150px;
        }
        .over{
            border:2px dashed #ccc;
            transform: scale(0.9, 0.9);
        }
    </style>
</head>
<body>
    <h3>請把文件夾中的圖片拖到下面區(qū)域</h3>
    
    <div id="box">
    </div>
    <script>
        //獲取目標(biāo)元素
        var box = document.querySelector("#box");
        //給目標(biāo)元素綁定事件
        box.addEventListener("dragenter", function(){
            this.classList.add('over');
        }, false);

        box.addEventListener("dragleave", function(){
            this.classList.remove("over");
        }, false);

        box.addEventListener("dragover", function(e){
            e.preventDefault();  //阻止默認(rèn)的動作
        }, false);

        box.addEventListener("drop", function(e){
            e.preventDefault(); //阻止瀏覽器默認(rèn)行為

            //遍歷FileList
            [].forEach.call(e.dataTransfer.files, function(itemFile){

                //創(chuàng)建imte
                var itemEle = document.createElement("div");
                itemEle.className = "item";
                document.getElementById("box").appendChild(itemEle);

                //讀取文件
                readImage(itemFile, itemEle);
                //上傳文件
                uploadImage(itemFile, itemEle);

            })
            this.classList.remove("over");  //恢復(fù)樣式
        },false);

        /**
         * 讀取圖片
         * @param File  fileObj 
         * @Param element ele  要預(yù)覽插入的元素
        */
        function readImage(fileObj, ele) {
            //創(chuàng)建FileReader對象
            var frObj = new FileReader();

            //監(jiān)聽讀取成功
            frObj.onload = function(){
                var img = document.createElement("img");
                img.src = frObj.result;
                ele.appendChild(img);
            }

            //讀取
            frObj.readAsDataURL(fileObj);
        }


        /**
         * 上傳文件
         * @param object FileObj對象
         * @param element ele對象
        */
        function uploadImage(fileObj, ele){
            //創(chuàng)建FormData
            var fd = new FormData();
            fd.append("uploadfile", fileObj);

            //創(chuàng)建元素
            var progressEle = document.createElement("div");
            progressEle.className = "item-progress";
            ele.appendChild(progressEle);

            progressEle.innerHTML = "0%";

            //XHR
            var xhr = new XMLHttpRequest();
            xhr.onload = function(){
                console.log(xhr.responseText);
                //上傳進(jìn)度去掉
                setTimeout(function(){
                    ele.removeChild(progressEle);
                }, 500)
            }
            //上傳對象
            xhr.upload.onprogress = function(en){
                progressEle.innerHTML = (en.loaded/en.total).toFixed(2)*100 + "%";
            }
            xhr.open("POST", "upload.php");
            xhr.send(fd);
        }
    </script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容