您好,欢迎来到颢想科技网。
搜索
您的当前位置:首页怎样使用JS实现文件拖拽上传

怎样使用JS实现文件拖拽上传

来源:颢想科技网
 这次给大家带来怎样使用JS实现文件拖拽上传,使用JS实现文件拖拽上传的注意事项有哪些,下面就是实战案例,一起来看一下。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS文件拖拽上传</title>
<style>
p{
 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,'base');
 return false;
 };
 };
</script>
</head>
<body>
<meter id="m1" value="0" min="0" max="100"></meter>
 <p id="box">请将文件拖拽到此区域</p>
</body>
</html>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

如何使用Vue整合AdminLTE模板

怎样用JS做出井字棋游戏

Copyright © 2019- haoxingyouxi.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务