Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix in multiple selections/drops #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions jquery.filedrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@
$.fn.filedrop = function(options) {
var opts = $.extend({}, default_opts, options),
global_progress = [],
doc_leave_timer, stop_loop = false,
files_count = 0,
files;
doc_leave_timer, stop_loop = false;

$('#' + opts.fallback_id).css({
display: 'none',
Expand All @@ -86,23 +84,41 @@
});

$('#' + opts.fallback_id).change(function(e) {

if (e.target.files.length == 0) {
return;
}
var files = [];

opts.drop(e);
files = e.target.files;
files_count = files.length;
upload();

for(var i=0;i<e.target.files.length;i++) {
files.push(e.target.files[i]);
}

upload(files);

});

function drop(e) {
if( opts.drop.call(this, e) === false ) return false;
if(!e.dataTransfer)
return;
files = e.dataTransfer.files;
if (files === null || files === undefined || files.length === 0) {

var newFiles = e.dataTransfer.files;
if (newFiles === null || newFiles === undefined || newFiles.length === 0) {
opts.error(errors[0]);
return false;
}
files_count = files.length;
upload();

var files = [];


for(var i=0;i<newFiles.length;i++) {
files.push(newFiles[i]);
}

upload(files);
e.preventDefault();
return false;
}
Expand Down Expand Up @@ -201,7 +217,8 @@
}

// Respond to an upload
function upload() {
function upload(files) {
var files_count = files.length;
stop_loop = false;

if (!files) {
Expand Down Expand Up @@ -261,9 +278,10 @@
};

// Process an upload, recursive
var process = function() {
var process = function(files) {

var fileIndex;
var files_count = files.length;

if (stop_loop) {
return false;
Expand Down Expand Up @@ -319,8 +337,8 @@
};
};

reader.onloadend = !opts.beforeSend ? send : function (e) {
opts.beforeSend(files[fileIndex], fileIndex, function () { send(e); });
reader.onloadend = !opts.beforeSend ? function(e) { send(e,files);} : function (e) {
opts.beforeSend(files[fileIndex], fileIndex, function () { send(e,files); });
};

reader.readAsDataURL(files[fileIndex]);
Expand All @@ -341,18 +359,19 @@

// If we still have work to do,
if (workQueue.length > 0) {
process();
process(files);
}
};

var send = function(e) {
var send = function(e,files) {

var files_count = files.length;
var fileIndex = (e.srcElement || e.target).index;

// Sometimes the index is not attached to the
// event object. Find it by size. Hack for sure.
if (e.target.index === undefined) {
e.target.index = getIndexBySize(e.total);
e.target.index = getIndexBySize(e.total,files);
}

var xhr = new XMLHttpRequest(),
Expand Down Expand Up @@ -460,17 +479,17 @@

// Pass any errors to the error option
if (xhr.status < 200 || xhr.status > 299) {
opts.error(xhr.statusText, file, fileIndex, xhr.status);
opts.error(xhr.statusText, file, fileIndex, xhr.status,xhr);
}
};
};

// Initiate the processing loop
process();
process(files);
}

function getIndexBySize(size) {
for (var i = 0; i < files_count; i++) {
function getIndexBySize(size,files) {
for (var i = 0; i < files.length; i++) {
if (files[i].size === size) {
return i;
}
Expand Down