手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:xheditor

HTML5拖放上传

html5中有一个特性就是以前一直常说的拖放上传,其实这个功能要实现的话真的不太难,但我对于DOM的操作并不是特别了解,因此我找了一些资料做备份
1、xheditor中的代码
2、html5的一些教程

xheditor中的代码很少,只有区区几行:

JavaScript代码[jBody是基中的一个元素对象,类似于$('#xxxdiv')]
  1. jBody.bind('dragenter dragover',function(ev){var types;if((types=ev.originalEvent.dataTransfer.types)&&$.inArray('Files', types)!==-1)return false;}).bind('drop',function(ev){  
  2.     var dataTransfer=ev.originalEvent.dataTransfer,fileList;  
  3.     if(dataTransfer&&(fileList=dataTransfer.files)&&fileList.length>0){  
  4.         var i,cmd,arrCmd=['Link','Img','Flash','Media'],arrExt=[],strExt;  
  5.         for(i in arrCmd){  
  6.             cmd=arrCmd[i];  
  7.             if(settings['up'+cmd+'Url']&&settings['up'+cmd+'Url'].match(/^[^!].*/i))arrExt.push(cmd+':,'+settings['up'+cmd+'Ext']);//允许上传  
  8.         }  
  9.         if(arrExt.length===0)return false;//禁止上传  
  10.         else strExt=arrExt.join(',');  
  11.         function getCmd(fileList){  
  12.             var match,fileExt,cmd;  
  13.             for(i=0;i<fileList.length;i++){  
  14.                 fileExt=fileList[i].fileName.replace(/.+\./,'');  
  15.                 if(match=strExt.match(new RegExp('(\\w+):[^:]*,'+fileExt+'(?:,|$)','i'))){  
  16.                     if(!cmd)cmd=match[1];  
  17.                     else if(cmd!==match[1])return 2;  
  18.                 }  
  19.                 else return 1;  
  20.             }  
  21.             return cmd;                   
  22.         }  
  23.         cmd=getCmd(fileList);  
  24.         if(cmd===1)alert('上传文件的扩展名必需为:'+strExt.replace(/\w+:,/g,''));  
  25.         else if(cmd===2)alert('每次只能拖放上传同一类型文件');  
  26.         else if(cmd){  
  27.             _this.startUpload(fileList,settings['up'+cmd+'Url'],'*',function(arrMsg){  
  28.                 var arrUrl=[],msg,onUpload=settings.onUpload;  
  29.                 if(onUpload)onUpload(arrMsg);//用户上传回调  
  30.                 for(i in arrMsg){  
  31.                     msg=arrMsg[i];  
  32.                     url=is(msg,'string')?msg:msg.url;  
  33.                     if(url.substr(0,1)==='!')url=url.substr(1);  
  34.                     arrUrl.push(url);  
  35.                 }  
  36.                 _this.exec(cmd);  
  37.                 $('#xhe'+cmd+'Url').val(arrUrl.join(' '));  
  38.                 $('#xheSave').click();  
  39.             });  
  40.         }  
  41.         return false;  
  42.     }  
  43. });  

也有这样的:HTML5拖放(Drag&Drop)API详细介绍和示例,当然这里我就不多贴代码了。不过文中最后一段是告诉我们有哪些事件可以用:

可以使用的新事件一共有这些:

dragstart
A drag has been initiated, with the dragged element as the event target.
drag
The mouse has moved, with the dragged element as the event target.
dragenter
The dragged element has been moved into a drop listener, with the drop listener element as the event target.
dragover
The dragged element has been moved over a drop listener, with the drop listener element as the event target. Since the default behavior is to cancel drops, returning false or calling preventDefault() in the event handler indicates that a drop is allowed here.
dragleave
The dragged element has been moved out of a drop listener, with the drop listener element as the event target.
drop
The dragged element has been successfully dropped on a drop listener, with the drop listener element as the event target.
dragend
A drag has been ended, successfully or not, with the dragged element as the event target.

并且注意能够被拖拽的元素一定要有这个属性:draggable=”true”

在web上面拖放一直以来都是个难题(strange beast),但是至少现在我们有一堆丰富的API了…. 所以也许这个方式会开始大行其道呢?

 

Tags: xheditor