Html5之图片拖放上传

来源:互联网 发布:建筑材料计算软件 编辑:程序博客网 时间:2024/05/01 07:33


  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title>HTML5 浏览器拖放 | HTML5 Drag and drop</title>  
  6.     <style>  
  7.         #section{font-family: "Georgia""微软雅黑""华文中宋";}  
  8.         .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;}  
  9.         .preview{max-width:360px;}  
  10.         #files-list{position:absolute;top:0;left:500px;}  
  11.         #list{width:460px;}  
  12.         #list .preview{max-width:250px;}  
  13.         #list p{color:#888;font-size:12px;}  
  14.         #list .green{color:#09c;}  
  15.     </style>  
  16. </head>  
  17. <body>  
  18.   
  19.     <div id="section">  
  20.         <p>把你的图片拖到下面的容器内:</p>  
  21.   
  22.         <div id="container" class="container">  
  23.               
  24.         </div>  
  25.         <div id ="files-list">  
  26.             <p>已经拖进过来的文件:</p>  
  27.             <ul id="list"></ul>  
  28.         </div>  
  29.     </div>  
  30.   
  31.     <script>  
  32.       
  33.     if (window.FileReader) {  
  34.   
  35.         var list = document.getElementById('list'),  
  36.             cnt = document.getElementById('container');  
  37.   
  38.         // 判断是否图片  
  39.         function isImage(type) {  
  40.             switch (type) {  
  41.             case 'image/jpeg':  
  42.             case 'image/png':  
  43.             case 'image/gif':  
  44.             case 'image/bmp':  
  45.             case 'image/jpg':  
  46.                 return true;  
  47.             default:  
  48.                 return false;  
  49.             }  
  50.         }  
  51.   
  52.         // 处理拖放文件列表  
  53.         function handleFileSelect(evt) {  
  54.             evt.stopPropagation();  
  55.             evt.preventDefault();  
  56.   
  57.             var files = evt.dataTransfer.files;  
  58.   
  59.             for (var i = 0, f; f = files[i]; i++) {  
  60.   
  61.                 var t = f.type ? f.type : 'n/a',  
  62.                     reader = new FileReader(),  
  63.                     looks = function (f, img) {  
  64.                         list.innerHTML += '<li><strong>' + f.name + '</strong> (' + t +  
  65.                             ') - ' + f.size + ' bytes<p>' + img + '</p></li>';  
  66.                         cnt.innerHTML = img;  
  67.                     },  
  68.                     isImg = isImage(t),  
  69.                     img;  
  70.   
  71.                 // 处理得到的图片  
  72.                 if (isImg) {  
  73.                     reader.onload = (function (theFile) {  
  74.                         return function (e) {  
  75.                             img = '<img class="preview" src="' + e.target.result + '" title="' + theFile.name + '"/>';  
  76.                             looks(theFile, img);  
  77.                         };  
  78.                     })(f)  
  79.                     reader.readAsDataURL(f);  
  80.                 } else {  
  81.                     img = '"o((>ω< ))o",你传进来的不是图片!!';  
  82.                     looks(f, img);  
  83.                 }  
  84.   
  85.             }  
  86.   
  87.         }  
  88.           
  89.         // 处理插入拖出效果  
  90.         function handleDragEnter(evt){ this.setAttribute('style''border-style:dashed;'); }  
  91.         function handleDragLeave(evt){ this.setAttribute('style'''); }  
  92.   
  93.         // 处理文件拖入事件,防止浏览器默认事件带来的重定向  
  94.         function handleDragOver(evt) {  
  95.             evt.stopPropagation();  
  96.             evt.preventDefault();  
  97.         }  
  98.           
  99.         cnt.addEventListener('dragenter', handleDragEnter, false);  
  100.         cnt.addEventListener('dragover', handleDragOver, false);  
  101.         cnt.addEventListener('drop', handleFileSelect, false);  
  102.         cnt.addEventListener('dragleave', handleDragLeave, false);  
  103.           
  104.     } else {  
  105.         document.getElementById('section').innerHTML = '你的浏览器不支持啊,同学';  
  106.     }  
  107.       
  108.     </script>  
  109.   
  110.       
  111. </body>  
  112. </html>  
原创粉丝点击