Upload AJAX select multiple files

3

This script below selects only one file for UPLOAD. I did a lot of research and found nothing that I could implement so I could select multiple files.

<script type="text/javascript" >
    $(function(){
        var btnUpload=$('#upload');
        var status=$('#status');
        new AjaxUpload(btnUpload, {
            // Arquivo que fará o upload
            action: 'upload-file.php',
            //Nome da caixa de entrada do arquivo
            name: 'uploadfile',
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // verificar a extensão de arquivo válido
                    status.text('Somente JPG, PNG ou GIF são permitidas');
                    return false;
                }
                status.text('Enviando...');
            },
            onComplete: function(file, response){
                //Limpamos o status
                status.text('');
                //Adicionar arquivo carregado na lista
                if(response==="success"){
                    $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
                } else{
                    $('<li></li>').appendTo('#files').text(file).addClass('error');
                }
            }
        });

    });
</script>

Does anyone know how to help me with this question?

    
asked by anonymous 30.12.2015 / 11:11

1 answer

4

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 ...

    
30.12.2015 / 11:52