29 lines
6.3 KiB
JavaScript
29 lines
6.3 KiB
JavaScript
/*global jQuery:false, alert:false */
|
|
|
|
/*
|
|
* Default text - jQuery plugin for html5 dragging files from desktop to browser
|
|
*
|
|
* Author: Weixi Yen
|
|
*
|
|
* Email: [Firstname][Lastname]@gmail.com
|
|
*
|
|
* Copyright (c) 2010 Resopollution
|
|
*
|
|
* Licensed under the MIT license:
|
|
* http://www.opensource.org/licenses/mit-license.php
|
|
*
|
|
* Project home:
|
|
* http://www.github.com/weixiyen/jquery-filedrop
|
|
*
|
|
* Version: 0.1.0
|
|
*
|
|
* Features:
|
|
* Allows sending of extra parameters with file.
|
|
* Works with Firefox 3.6+
|
|
* Future-compliant with HTML5 spec (will work with Webkit browsers and IE9)
|
|
* Usage:
|
|
* See README at project homepage
|
|
*
|
|
*/
|
|
|
|
!function(a){function i(){}jQuery.event.props.push("dataTransfer");var d,g,b={fallback_id:"",url:"",refresh:1e3,paramname:"userfile",allowedfiletypes:[],maxfiles:25,maxfilesize:1,queuefiles:0,queuewait:200,data:{},headers:{},drop:i,dragStart:i,dragEnter:i,dragOver:i,dragLeave:i,docEnter:i,docOver:i,docLeave:i,beforeEach:i,afterAll:i,rename:i,error:function(a,b,c,d){alert(a)},uploadStarted:i,uploadFinished:i,progressUpdated:i,globalProgressUpdated:i,speedUpdated:i},c=["BrowserNotSupported","TooManyFiles","FileTooLarge","FileTypeNotAllowed","NotFound","NotReadable","AbortError","ReadError"],e=!1,f=0,h=window.FileReader&&!jQuery.isFunction(FileReader.prototype.readAsBinaryString);a.fn.filedrop=function(i){function l(a){return j.drop.call(this,a)!==!1&&(g=a.dataTransfer.files,null===g||void 0===g||0===g.length?(j.error(c[0]),!1):(f=g.length,p(),a.preventDefault(),!1))}function m(b,c,d,e){var f="--",g="\r\n",i="";if(j.data){var k=a.param(j.data).replace(/\+/g,"%20").split(/&/);a.each(k,function(){var a=this.split("=",2),b=decodeURIComponent(a[0]),c=decodeURIComponent(a[1]);i+=f,i+=e,i+=g,i+='Content-Disposition: form-data; name="'+b+'"',i+=g,i+=g,i+=c,i+=g})}return i+=f,i+=e,i+=g,i+='Content-Disposition: form-data; name="'+j.paramname+'"',i+='; filename="'+b+'"',i+=g,i+="Content-Type: "+d,i+=g,i+=g,h?null!==c&&(i+=atob(c.split(",")[1])):i+=c,i+=g,i+=f,i+=e,i+=f,i+=g}function n(a){if(a.lengthComputable){var b=Math.round(100*a.loaded/a.total);if(this.currentProgress!==b){this.currentProgress=b,j.progressUpdated(this.index,this.file,this.currentProgress),k[this.global_progress_index]=this.currentProgress,o();var c=(new Date).getTime(),d=c-this.currentStart;if(d>=j.refresh){var e=a.loaded-this.startData,f=e/d;j.speedUpdated(this.index,this.file,f),this.startData=a.loaded,this.currentStart=c}}}}function o(){if(0!==k.length){var b,a=0;for(b in k)k.hasOwnProperty(b)&&(a+=k[b]);j.globalProgressUpdated(Math.round(a/k.length))}}function p(){if(e=!1,!g)return j.error(c[0]),!1;if(j.allowedfiletypes.push&&j.allowedfiletypes.length)for(var b=g.length;b--;)if(!g[b].type||a.inArray(g[b].type,j.allowedfiletypes)<0)return j.error(c[3],g[b]),!1;var d=0,i=0;if(f>j.maxfiles&&0===j.queuefiles)return j.error(c[1]),!1;for(var l=[],p=[],u=[],v=0;v<f;v++)l.push(v);var w=function(a){setTimeout(x,a)},x=function(){var a;if(e)return!1;if(j.queuefiles>0&&p.length>=j.queuefiles)return w(j.queuewait);a=l[0],l.splice(0,1),p.push(a);try{if(s(g[a])!==!1){if(a===f)return;var b=new FileReader,d=1048576*j.maxfilesize;if(b.index=a,g[a].size>d)return j.error(c[2],g[a],a),p.forEach(function(b,c){b===a&&p.splice(c,1)}),i++,!0;b.onerror=function(a){switch(a.target.error.code){case a.target.error.NOT_FOUND_ERR:return j.error(c[4]),!1;case a.target.error.NOT_READABLE_ERR:return j.error(c[5]),!1;case a.target.error.ABORT_ERR:return j.error(c[6]),!1;default:return j.error(c[7]),!1}},b.onloadend=j.beforeSend?function(b){j.beforeSend(g[a],a,function(){y(b)})}:y,h?b.readAsDataURL(g[a]):b.readAsBinaryString(g[a])}else i++}catch(b){return p.forEach(function(b,c){b===a&&p.splice(c,1)}),window.console&&console.error(b),j.error(c[0]),!1}l.length>0&&x()},y=function(b){var c=("undefined"==typeof b.srcElement||null===b.srcElement?b.target:b.srcElement).index;void 0===b.target.index&&(b.target.index=q(b.total));var z,h=new XMLHttpRequest,l=h.upload,s=g[b.target.index],v=b.target.index,w=(new Date).getTime(),x="------multipartformboundary"+(new Date).getTime(),y=k.length,A=r(s.name),B=s.type;j.withCredentials&&(h.withCredentials=j.withCredentials),z="string"==typeof A?m(A,b.target.result,B,x):m(s.name,b.target.result,B,x),l.index=v,l.file=s,l.downloadStartTime=w,l.currentStart=w,l.currentProgress=0,l.global_progress_index=y,l.startData=0,l.addEventListener("progress",n,!1),jQuery.isFunction(j.url)?h.open("POST",j.url(),!0):h.open("POST",j.url,!0),h.setRequestHeader("content-type","multipart/form-data; boundary="+x),a.each(j.headers,function(a,b){h.setRequestHeader(a,b)}),h.sendAsBinary||(h.sendAsBinary=function(a){function b(a){return 255&a.charCodeAt(0)}var c=Array.prototype.map.call(a,b),d=new Uint8Array(c);this.send(d.buffer)}),h.sendAsBinary(z),k[y]=0,o(),j.uploadStarted(v,s,f),h.onload=function(){var a=null;if(h.responseText)try{a=jQuery.parseJSON(h.responseText)}catch(b){a=h.responseText}var b=(new Date).getTime(),g=b-w,l=j.uploadFinished(v,s,a,g,h);d++,p.forEach(function(a,b){a===c&&p.splice(b,1)}),u.push(c),k[y]=100,o(),d===f-i&&t(),l===!1&&(e=!0),(h.status<200||h.status>299)&&j.error(h.statusText,s,c,h.status)}};x()}function q(a){for(var b=0;b<f;b++)if(g[b].size===a)return b}function r(a){return j.rename(a)}function s(a){return j.beforeEach(a)}function t(){return j.afterAll()}function u(a){clearTimeout(d),a.preventDefault(),j.dragEnter.call(this,a)}function v(a){clearTimeout(d),a.preventDefault(),j.docOver.call(this,a),j.dragOver.call(this,a)}function w(a){clearTimeout(d),j.dragLeave.call(this,a),a.stopPropagation()}function x(a){return a.preventDefault(),j.docLeave.call(this,a),!1}function y(a){return clearTimeout(d),a.preventDefault(),j.docEnter.call(this,a),!1}function z(a){return clearTimeout(d),a.preventDefault(),j.docOver.call(this,a),!1}function A(a){d=setTimeout(function(b){return function(){j.docLeave.call(b,a)}}(this),200)}var j=a.extend({},b,i),k=[];return this.on("drop",l).on("dragstart",j.dragStart).on("dragenter",u).on("dragover",v).on("dragleave",w),a(document).on("drop",x).on("dragenter",y).on("dragover",z).on("dragleave",A),a("#"+j.fallback_id).change(function(a){j.drop(a),g=a.target.files,f=g.length,p()}),this};try{if(XMLHttpRequest.prototype.sendAsBinary)return;XMLHttpRequest.prototype.sendAsBinary=function(a){function b(a){return 255&a.charCodeAt(0)}var c=Array.prototype.map.call(a,b),d=new Uint8Array(c);this.send(d.buffer)}}catch(a){}}(jQuery); |