Blueimp - fileupload. upload many stored images

0

Hello community, I have the following problem with blueimp fileupload . I want to upload a single image to the server. But when I select in the input type file several times and select different images I receive all those images as if it were stored in the input the list of images. In my input delete the attribute múltiple and name="file" did not work.

my jquery code is:

$('#photo_news').fileupload({
            url: '/_uploader/gallery/upload',
            type: 'POST',
            multipart: true,
            dataType: 'json',
            autoUpload: false,
            singleFileUploads: true,
            maxNumberOfFiles : 1,
            replaceFileInput: false,
            add: function(e, data){



                $("#form_news").submit(function(e){

                    e.preventDefault();

                    data.formData = $("#form_news").serializeArray();
                    data.submit();


                });

            },
            done: function(e, data) {

                if(data){
                    window.location.reload();
                }

            },

            error: function(e, data){

                console.log(data);

            }

        });
    
asked by edinson carranza saldaña 29.01.2018 в 15:11
source

2 answers

0

I'll show you another way to send the input to the server, and in this way we will control that it is the last selected element, you can keep the property name="file" that you had previously:

$('#upload-input').on('change', function () {
       //files tendrá todos los elementos que sean seleccionados, o el único que se seleccione
    var files = $(this).get(0).files;

        var formData = new FormData();
              //si se seleccionaron varios elementos file sera el ultimo que se selecciono
        var file = files[files.length-1];
        formData.append('file', file, file.name);


        $.ajax({
            url: '/_uploader/gallery/upload',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            xhr: function () {
                //este xhr es por si quieres llenar alguna barra de progreso
                var xhr = new XMLHttpRequest();
                xhr.upload.addEventListener('progress', function (evt) {
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;
                        percentComplete = parseInt(percentComplete * 100); // esta variable contiene por que % esta la subida
                        if (percentComplete === 100) {
                            // aquí pones que ocurrirá cuando el % sea 100%
                        }
                    }
                }, false);
                return xhr;
            }
        }).done(function (data) {
            //aquí declaras lo que ocurrirá cuando se termine de subir el fichero
        });

});
  

Note: remember that if you are using your input to upload only images, input you can include the accept property to define which types of upload images, for example accept="image/png, .jpeg, .jpg, image/gif"

     
    

Keep in mind that with what you implement previously at the time the selection is made, auto-followed the request is made and sent to the server, if you want to separate it, you would have to put the function $.ajax() in a function apart, and call it when you want it sent.

  
    
answered by 29.01.2018 в 16:00
0

Try differently since the idea is to make it work with the library since I use a bundle for symfony and work together.

probe as follows in this code where you only send one image and it does not repeat. but take the first image and not the last one.

before submit in add. I put the input file disable with fileupload ...

$('#photo_news').fileupload({
            url: '/_uploader/gallery/upload',
            type: 'POST',
            multipart: true,
            dataType: 'json',
            autoUpload: false,
            singleFileUploads: true,
            replaceFileInput: false,
            maxNumberOfFiles: 1,

            add: function(e, data){

                $("#photo_news").fileupload('disable');

                $("#form_news").submit(function(e){

                    e.preventDefault();

                    data.formData = $("#form_news").serializeArray();
                    data.submit();


                });

            },
            done: function(e, data) {

                if(data){
                    window.location.reload();
                }

            },

            error: function(e, data){

                console.log(data);

            }

        });
    
answered by 29.01.2018 в 16:10