(function ($) {
$.fn.upload = function(options) {
var defer = $.Deferred();
var settings = {
url: null,
urlBase: null
};
if (options) {
$.extend(settings, options);
}
var $this = $(this);
$this.on('dragenter dragover', function() {
return false;
});
$this.on('drop', function(e) {
e.preventDefault();
var file = e.originalEvent.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var xhr = new XMLHttpRequest();
var url = settings.url;
xhr.open('POST', url, true);
var filetype = file.type;
if (_.isEmpty(filetype)) {
filetype = 'application/octet-stream';
}
xhr.setRequestHeader('Content-Type', filetype);
xhr.onload = function(e) {
if (this.status === 200) {
return defer.resolve();
} else {
return defer.reject();
}
};
xhr.send(file);
};
reader.readAsBinaryString(file);
});
return defer.promise();
};
})(jQuery);
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme