Uploading multiple files requires some consideration.
- Do you have an input
multiple
for all different files or inputs for each file?
- Do you want to upload in series or concurrent (sequential)?
Multiple inputs, 1 file per input
In case of multiple files but each with your input in this case is simple, and you just have to create a loop in your code.
competing submissions:
var inputs = $('.inputs');
var status = $('#status');
inputs.get().forEach(function(input, i) {
// aqui podes usar o "i" se só quiseres saber o numero
// eu vou usar o nome do ficheiro
var ficheiro = input.files[0];
$.ajax({
url: 'upload-file.php',
contentType: false,
processData: false,
data: (new FormData()).append('file', ficheiro),
link
In this case it immediately triggers parallel (competing) requests. It's the fastest way.
consecutive serial submissions:
var inputs = $('.inputs').get();
var status = $('#status');
function proximoAjax() {
var proximo = inputs.shift();
var data = (new FormData()).append('file', proximo.files[0])
$.ajax({
url: 'upload-file.php',
contentType: false,
processData: false,
data: data,
beforeSend: function(file, ext) {
// etc...
},
success: function(file, response) {
proximoAjax(); // <----
// etc...
}
});
}
proximoAjax();
link
Unique input, with attribute multiple
, n files per input
competing submissions:
var input = $('#input');
var status = $('#status');
var ficheiros = input.get().map(function(input, i) {
returninput.files[i];
});
ficheiros.forEach(function(ficheiro) {
$.ajax({
url: 'upload-file.php',
contentType: false,
processData: false,
data: (new FormData()).append('file', ficheiro),
link
consecutive serial submissions:
var input = $('#input');
var status = $('#status');
var ficheiros = input.get().map(function(input, i) {
returninput.files[i];
});
function proximoAjax() {
var proximo = ficheiros.shift();
var data = (new FormData()).append('file', proximo)
$.ajax({
url: 'upload-file.php',
contentType: false,
processData: false,
data: data,
beforeSend: function(file, ext) {
// etc...
},
success: function(file, response) {
proximoAjax(); // <----
// etc...
}
});
}
proximoAjax();
link
Notes:
# 1
This answer points out differences and possibilities. I did not exactly test your code because I do not have access to it. This way, using%% of jQuery and not your%% abstraction I think it is also easier to maintain, and useful to others who like me do not know what this $.ajax
does.
# 2
When I use new AjaxUpload()
it is to convert a jQuery object to a Array native. Easier to work with and know how to use the result as I see it.
# 3
The main difference in a simple input new AjaxUpload()
and an input that accepts multiple files .get()
is (in addition to the <input type="file" />
attribute) that the <input type="file" multiple />
property of the element has the names saved. That is, a simple input only has multiple
whereas an input .files
has .files[0]
, multiple
, etc ...