Created
September 25, 2014 05:59
-
-
Save piouc/b7796227c40118638a7a to your computer and use it in GitHub Desktop.
SequentialUploader.js
This file contains hidden or 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 SequentialUploader(url, name){ | |
// local vars | |
var files = []; | |
var uploading = false; | |
// public method | |
this.add = function(file, options){ | |
console.log('new file'); | |
files.push({ | |
file: file, | |
options: options || {} | |
}); | |
if(!uploading){ | |
upload(files); // strat upload | |
} | |
}; | |
// private method | |
function upload(files){ | |
console.log(files[0]); | |
if(_.isEmpty(files)) {//残りのファイルが無い場合終了する | |
console.log('uploaded'); | |
uploading = false; | |
return ; | |
} | |
console.log('start uploading'); | |
uploading = uploading || true; | |
var file = files.shift(); | |
var formData = new FormData(); | |
formData.append(name, files.file); | |
$.ajax({ | |
method: 'POST', | |
url: url, | |
cache: false, | |
contentType: false, | |
processData: false, | |
data: formData, | |
dataType: 'json', | |
success: function(res, status, xhr){ | |
if(file.options.success){ | |
file.options.success(res, status, xhr); | |
} | |
upload(files); | |
}, | |
error: function(res, status, error){ | |
console.log('error'); | |
if(file.options.error){ | |
file.options.error(res, status, error); | |
} | |
upload(files); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment