转行学开发,代码100天——2018-04-10

今天记录一个利用JavaScript实现文件拖拽上传到浏览器,后天将文件打开的小案例。

基本功能:1点击添加文件 2 文件拖拽添加

html:

<div id="box">
<span id="span">选择或拖放文件</span>
<input id="browse" type="file">
</div>

css:

      #box{
position: relative;
border:3px dashed #ddd;
width: 200px;
height: 70px;
text-align: center;
line-height: 70px;
cursor: pointer;
}
#box input{
position: absolute;
top:;
left:;
width: 100%;
height: 100%;
opacity:;
cursor: pointer;
}
#box.hover{
border:3px solid #999;
}

javascript:

<script type="text/javascript">
var box = document.getElementById("box");
var input = document.getElementById("browse");
var span = document.getElementById("span");
//box 事件
box.ondragover = function()
{
this.className = "box hover"; }
box.ondragleave = function()
{
this.className = "box";
} //input 事件 input.ondragover = function(e)
{
e.preventDefault(); }
input.ondrop = function(e)
{
e.preventDefault();
box.className = "box";
var file = e.dataTransfer.files[0];
show(file);
}
input.onchange = function()
{
var file = this.files[0];
show(file);
}
function show(file)
{
span.innerHTML = file.name;
var fr = new FileReader();
fr.onload = function()
{
var result = this.result;
console.log(result);
}
fr.readAsText(file);
} </script>

案例效果:

最新文章

  1. padding/border与width的关系
  2. Qt 常用的功能
  3. json入门(二)
  4. 将Word转为带书签的PDF
  5. webservice引用spring的bean
  6. C Primer Plus之文件输入/输出
  7. Debian下配置SSH服务器的方法
  8. HTML:form表单总结,input,select,option,textarea,label
  9. MongoDB 权限管理 用户名和密码的操作
  10. 在Eclipse中格式化Android代码
  11. Android(java)学习笔记258:JNI之hello.c(c代码功能实现)指针语法解析
  12. Quickly Start Listener scripts
  13. Swift计算字符数量
  14. href设置action绝对路径和相对路径
  15. PowerShell 脚本中的密码
  16. OpenShift中的持续交付
  17. Linux小记 — Ubuntu自动化配置
  18. Laravel5.5核心架构理解
  19. RabbitMQ 设置消息的TTL(过期时间)
  20. jsp的9大内置对象和4大作用域

热门文章

  1. HDFS-HA高可用工作机制
  2. Java Collection总结
  3. 攻防世界--re1-100
  4. sqoop简单使用
  5. Spark MLlib机器学习(一)——决策树
  6. 完整项目:网上图书商城(一、MySQL数据库设计)未完
  7. python使用etcd
  8. file 显示文件的类型
  9. 了解卷积神经网络如何使用TDA学习
  10. hive之wordcount