Created
December 21, 2011 07:16
-
-
Save mmarcon/1505038 to your computer and use it in GitHub Desktop.
Draft of a simple jQuery plugin to upload files asynchronously via AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var settings = { | |
hoverClass: 'sUpload-hover', | |
dropAreaClass: 'sUpload-drop', | |
dropMessage: 'Drop file here!', | |
dropAreaHeight: 200, uploadURL: null, | |
fileReaderCallback: null, | |
fileUploadCallback: null, | |
errorCallback: null | |
}, | |
methods = { init: function(options) { | |
if ($.browser.msie) { | |
return; | |
} | |
if (options) { | |
$.extend(settings, options); } | |
if ($('body').data('sUpload') && $('body').data('sUpload').enabled) { | |
return; | |
} | |
$('body').data('sUpload', {enabled: true}); | |
return this.each(function() { var that = $(this), | |
timer, | |
stopDragging = function(){ | |
clearInterval (timer); | |
that.trigger('dragend'); | |
}; dropTarget = $('<div/>').addClass(settings.dropAreaClass); | |
dropTarget.append('<p>' + settings.dropMessage + '</p>'); | |
//Append drop target | |
that.append(dropTarget); | |
//Bind events | |
that.bind('dragover', function (e) { e = e.originalEvent; | |
/* | |
if (!e.dataTransfer.types) { | |
return false; | |
} | |
if (e.dataTransfer.types.contains && !e.dataTransfer.types.contains ('Files')) { return false; | |
} | |
if (e.dataTransfer.types.indexOf && e.dataTransfer.types.indexOf ('Files') === -1) { | |
return false; | |
} | |
*/ if (!$(this).hasClass(settings.hoverClass)) { | |
$(this).addClass(settings.hoverClass); | |
dropTarget.show().animate({ | |
height: settings.dropAreaHeight | |
}, 500); | |
} clearInterval (timer); | |
timer = setInterval(stopDragging, 700); | |
return false; | |
}).bind('dragend', function() { | |
$(this).removeClass(settings.hoverClass); | |
dropTarget.hide().height(0); return false; | |
}); | |
dropTarget.bind('dragend', function(){ | |
var me = $(this); | |
me.parent().removeClass(settings.hoverClass); | |
me.hide().height(0); return false; | |
}); | |
dropTarget.bind('drop', function(e) { | |
var me = $(this), e = e.originalEvent; | |
e.preventDefault(); | |
me.parent().removeClass(settings.hoverClass); me.hide().height(0); | |
var file = e.dataTransfer.files[0], reader, xhr, fdata; | |
if ($.isFunction(settings.fileReaderCallback)) { | |
reader = new FileReader(); | |
reader.onLoad = settings.fileReaderCallback; | |
reader.readAsDataURL(file); } | |
fdata = new FormData(); | |
fdata.append('file0', file); | |
xhr = new XMLHttpRequest(); | |
xhr.open ('POST', settings.uploadURL, true); | |
xhr.onload = settings.fileUploadCallback; xhr.fileName = file.fileName; //So the onload callback will pass it as part of the XMLHTTPObject (this) | |
xhr.onerror = settings.errorCallback; | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
xhr.send (fdata); | |
return false; | |
}); }); | |
}, | |
updateURL: function (newURL) { | |
if (!newURL) { | |
$.error('This method needs a new URL passed as a parameter.'); | |
return false; } | |
$.extend(settings, {uploadURL: newURL}); | |
} | |
}; $.fn.sUpload = function(method) { | |
if (methods[method]) { | |
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | |
} else if (typeof method === 'object' || !method) { | |
return methods.init.apply(this, arguments); | |
} else { $.error('Method ' + method + ' does not exist on jQuery.film'); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment