There is a js script that loads the selected file by clicking on #uploadImage .

It is necessary that the file is loaded without pressing, immediately after selecting the file.

HTML:

 <input type="file" class="file-styled" name="image" multiple="multiple" accept=".txt,image/*"><button class="btn bg-slate" id="uploadImage">Загрузить</button> 

Js:

 (function($){ var files; $('input[type=file]').on('change', function(){ files = this.files; }); $('#uploadImage').on( 'click', function( event ){ event.stopPropagation(); event.preventDefault(); if( typeof files == 'undefined' ) return; var data = new FormData(); $.each( files, function( key, value ){ data.append( key, value ); }); data.append( 'uploadImage', 1 ); $.ajax({ url : window.location.href, type : 'POST', data : data, cache : false, dataType : 'json', processData : false, contentType : false, success : function( respond, status, jqXHR ){ if( typeof respond.error === 'undefined' ){ var files_path = respond.files; var html = 'Файл успешно загружен'; $('.ajax-reply').html( html ); } else { console.log('ОШИБКА: ' + respond.error ); } }, error: function( jqXHR, status, errorThrown ){ console.log( 'ОШИБКА AJAX запроса: ' + status, jqXHR ); } }); }); })(jQuery) 

Please help in the implementation!

  • $('#uploadImage').on( 'click', function( event ){ replace with $('#uploadImage').on( 'change', function( event ){ - InDevX
  • #uploadImage this is the button - verstala
  • 2
    Send here $('input[type=file]').on('change', function(){ - Alexey Kapustsin am
  • Thank you works. - verstala

0