A
A
Alexey2015-09-24 23:49:27
PHP
Alexey, 2015-09-24 23:49:27

Why does ajax upload send multiple files?

I downloaded a plugin for ajax file upload, this one: https://github.com/burt202/lite-uploader/blob/mast... I inserted
the following code on the page:

$('.fileUpload').live('click', function(){
      $(this).liteUploader({
      script: '/upload_images/'
      })
      .on('lu:progress', function (e, percentage) {
        $(this).parent().find('.upimtx .progressbar').css('width', percentage + '%');
      })
      .on('lu:success', function (e, response) {
        alert(response);
        obj = jQuery.parseJSON(response);
        $(this).parent().find("img").remove();
        $(this).parent().find(".images_hidden").val(obj.imagename);
        $(this).parent().prepend(obj.image);
      });
    });

And everything seems to be fine, it sends files to the server, but if you select a file, then everything is fine, if you select another file in the same input, then 2 files are sent to the server, if the third time, then already 3 and so on. I can't figure out why...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2015-09-25
@dzheka3d

For starters, forget about live('click'), on() has been used for a long time.
You initialize the uploader every time, why?
Why the whole live click wrapper, instead of $('.file').liteUploader()

A
Alexey, 2015-09-25
@dzheka3d

I don't know, it's probably a crutch, but that's how it turned out for me

function in_upload(box){ // инициализация для загрузки через ajax
    $(box).liteUploader({
      script: '/upload_images/'
      })
      .on('lu:progress', function (e, percentage) {
        $(this).parent().find('.upimtx .progressbar').css('width', percentage + '%');
      })
      .on('lu:success', function (e, response) {
          obj = jQuery.parseJSON(response);
          $(this).parent().find("img").remove();
          $(this).parent().find(".images_hidden").val(obj.imagename);
          $(this).parent().prepend(obj.image);
    });
  } 

in_upload('#fileUpload_' + index); // вызываем функцию инициализации

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question