Html5之图片拖放上传
2011-12-07 14:22
218 查看
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 浏览器拖放 | HTML5 Drag and drop</title>
<style>
#section{font-family: "Georgia", "微软雅黑", "华文中宋";}
.container{display:inline-block;min-height:200px;min-width:360px;color:#f30;padding:30px;border:3px solid #ddd;-moz-border-radius:10px;-webkit-border-radius:10px;border-radius:10px;}
.preview{max-width:360px;}
#files-list{position:absolute;top:0;left:500px;}
#list{width:460px;}
#list .preview{max-width:250px;}
#list p{color:#888;font-size:12px;}
#list .green{color:#09c;}
</style>
</head>
<body>
<div id="section">
<p>把你的图片拖到下面的容器内:</p>
<div id="container" class="container">
</div>
<div id ="files-list">
<p>已经拖进过来的文件:</p>
<ul id="list"></ul>
</div>
</div>
<script>
if (window.FileReader) {
var list = document.getElementById('list'),
cnt = document.getElementById('container');
// 判断是否图片
function isImage(type) {
switch (type) {
case 'image/jpeg':
case 'image/png':
case 'image/gif':
case 'image/bmp':
case 'image/jpg':
return true;
default:
return false;
}
}
// 处理拖放文件列表
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
var t = f.type ? f.type : 'n/a',
reader = new FileReader(),
looks = function (f, img) {
list.innerHTML += '<li><strong>' + f.name + '</strong> (' + t +
') - ' + f.size + ' bytes<p>' + img + '</p></li>';
cnt.innerHTML = img;
},
isImg = isImage(t),
img;
// 处理得到的图片
if (isImg) {
reader.onload = (function (theFile) {
return function (e) {
img = '<img class="preview" src="' + e.target.result + '" title="' + theFile.name + '"/>';
looks(theFile, img);
};
})(f)
reader.readAsDataURL(f);
} else {
img = '"o((>ω< ))o",你传进来的不是图片!!';
looks(f, img);
}
}
}
// 处理插入拖出效果
function handleDragEnter(evt){ this.setAttribute('style', 'border-style:dashed;'); }
function handleDragLeave(evt){ this.setAttribute('style', ''); }
// 处理文件拖入事件,防止浏览器默认事件带来的重定向
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
}
cnt.addEventListener('dragenter', handleDragEnter, false);
cnt.addEventListener('dragover', handleDragOver, false);
cnt.addEventListener('drop', handleFileSelect, false);
cnt.addEventListener('dragleave', handleDragLeave, false);
} else {
document.getElementById('section').innerHTML = '你的浏览器不支持啊,同学';
}
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 浏览器拖放 | HTML5 Drag and drop</title>
<style>
#section{font-family: "Georgia", "微软雅黑", "华文中宋";}
.container{display:inline-block;min-height:200px;min-width:360px;color:#f30;padding:30px;border:3px solid #ddd;-moz-border-radius:10px;-webkit-border-radius:10px;border-radius:10px;}
.preview{max-width:360px;}
#files-list{position:absolute;top:0;left:500px;}
#list{width:460px;}
#list .preview{max-width:250px;}
#list p{color:#888;font-size:12px;}
#list .green{color:#09c;}
</style>
</head>
<body>
<div id="section">
<p>把你的图片拖到下面的容器内:</p>
<div id="container" class="container">
</div>
<div id ="files-list">
<p>已经拖进过来的文件:</p>
<ul id="list"></ul>
</div>
</div>
<script>
if (window.FileReader) {
var list = document.getElementById('list'),
cnt = document.getElementById('container');
// 判断是否图片
function isImage(type) {
switch (type) {
case 'image/jpeg':
case 'image/png':
case 'image/gif':
case 'image/bmp':
case 'image/jpg':
return true;
default:
return false;
}
}
// 处理拖放文件列表
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
var t = f.type ? f.type : 'n/a',
reader = new FileReader(),
looks = function (f, img) {
list.innerHTML += '<li><strong>' + f.name + '</strong> (' + t +
') - ' + f.size + ' bytes<p>' + img + '</p></li>';
cnt.innerHTML = img;
},
isImg = isImage(t),
img;
// 处理得到的图片
if (isImg) {
reader.onload = (function (theFile) {
return function (e) {
img = '<img class="preview" src="' + e.target.result + '" title="' + theFile.name + '"/>';
looks(theFile, img);
};
})(f)
reader.readAsDataURL(f);
} else {
img = '"o((>ω< ))o",你传进来的不是图片!!';
looks(f, img);
}
}
}
// 处理插入拖出效果
function handleDragEnter(evt){ this.setAttribute('style', 'border-style:dashed;'); }
function handleDragLeave(evt){ this.setAttribute('style', ''); }
// 处理文件拖入事件,防止浏览器默认事件带来的重定向
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
}
cnt.addEventListener('dragenter', handleDragEnter, false);
cnt.addEventListener('dragover', handleDragOver, false);
cnt.addEventListener('drop', handleFileSelect, false);
cnt.addEventListener('dragleave', handleDragLeave, false);
} else {
document.getElementById('section').innerHTML = '你的浏览器不支持啊,同学';
}
</script>
</body>
</html>
相关文章推荐
- Html5 图片拖放上传
- html5之文件与图片拖放上传
- Html5 拖放上传图片
- HTML5图片 拖放上传
- php一次上传多张图片,html5 multiple的使用。
- 深入研究HTML5实现图片压缩上传
- html5上传图片立即看到效果和图片替换
- HTML5实现图片文件异步上传
- html5获取上传图片的大小和即时显示
- 用HTML5的File API做上传图片预览功能
- 用html5的file api做一个简单的上传图片预览
- HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术
- html5图片上传预览示例分享
- 用HTML5获取摄像头图像,并且上传图片到服务器(代码完整可用)
- HTML5接口 实现input上传图片预览
- HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术(转)
- 基于HTML5的可预览多图片Ajax上传
- HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)
- 【小月博客】 Html5 上传图片 移动端、PC端通用
- php+html5使用FormData对象提交表单及上传图片的方法