JS实现的文件拖拽上传功能示例
        网络      2018-06-11     1778 
        本文实例讲述了JS实现的文件拖拽上传功能。分享给大家供大家参考,具体如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <!doctype html> <html> <head> <meta charset="utf-8"> <title>www.jb51.net JS文件拖拽上传</title> <style> div{  width: 300px;  height: 300px;  border:1px dashed #000;  position:absolute;  top: 50%;  left: 50%;  margin:-150px 0 0 -150px;  text-align:center;  font:20px/300px '微软雅黑';  display:none; } </style> <script>  window.onload = function () {   var oBox = document.getElementById('box');   var oM = document.getElementById('m1');   var timer = null;   document.ondragover = function(){    clearTimeout(timer);    timer = setTimeout(function(){     oBox.style.display = 'none';    },200);    oBox.style.display = 'block';   };   //进入子集的时候 会触发ondragover 频繁触发 不给ondrop机会   oBox.ondragenter = function(){    oBox.innerHTML = '请释放鼠标';   };   oBox.ondragover = function(){    return false;   };   oBox.ondragleave = function(){    oBox.innerHTML = '请将文件拖拽到此区域';   };   oBox.ondrop = function(ev){    var oFile = ev.dataTransfer.files[0];    var reader = new FileReader();    //读取成功    reader.onload = function(){     console.log(reader);    };    reader.onloadstart = function(){     alert('读取开始');    };    reader.onloadend = function(){     alert('读取结束');    };    reader.onabort = function(){     alert('中断');    };    reader.onerror = function(){     alert('读取失败');    };    reader.onprogress = function(ev){     var scale = ev.loaded/ev.total;     if(scale>=0.5){      alert(1);      reader.abort();     }     oM.value = scale*100;    };    reader.readAsDataURL(oFile,'base64');    return false;   };  }; </script> </head> <body> <meter id="m1" value="0" min="0" max="100"></meter>  <div id="box">请将文件拖拽到此区域</div> </body> </html> | 
使用http://tools.jb51.net/code/HtmlJsRun在线运行测试效果如下:
上一篇:政府补助的税会处理,你搞懂了么?

