Ketika saya meletakkan file di input html saya ingin mengunggah melalui posting ajax. (tanpa klik) Ini berfungsi setelah saya mengklik tombol boot dalam kode saya saat ini. kode saya

    $(document).ready(function () {
  $("#but_upload").click(function () {
    $('#img-loag-scrin').html('');
    $('div#img-loag-scrin').css('display', 'block').prepend('<img id="img_akb" src="https://' + HTTP + '/images/ajax-loader.gif" id="buff-load" style="display:block;margin:10px auto; cursor: wait;">');
    var fd = new FormData();
    var files = $('#file')[0].files[0];
    fd.append('file', files);
    $.ajax({
      url: 'https://localhost.uz/ajax/ajax_img.php?act=upload',
      dataType: "json",
      type: 'post',
      cache: false,
      data: fd,
      contentType: false,
      processData: false,
      success: function (response) {
        if (response != 0) {
          $('#img-loag-scrin').prepend(response.scrins);
          $('#textrea').val($('#textrea').val() + " " + response.textrea);
          $('#img_akb').remove();
        } else {
          alert('File not uploaded');
        }
      }
    });
  });
});

KODE HTML

<form method="post" action="" enctype="multipart/form-data" id="myform">
  <input type="file" id="file" name="file">
  <input type="button" class="sf_button" value="Upload" id="but_upload">
</form>

Ketika html dimasukkan ke dalam file input, tanpa mengklik tombol, file tersebut harus dimuat. Setelah gambar dimuat, gambar harus hilang dari input

0
Akbarali 18 Maret 2020, 08:09

1 menjawab

Jawaban Terbaik

Saya telah menggunakan perubahan.

$(document).ready(function () {
  $('#file').unbind().on('change', function () {
    $('#img-loag-scrin').html('');
    $('div#img-loag-scrin').css('display', 'block').prepend('<img id="img_akb" src="https://' + HTTP + '/images/ajax-loader.gif" id="buff-load" style="display:block;margin:10px auto; cursor: wait;">');
    var fd = new FormData();
    var files = $('#file')[0].files[0];
    fd.append('file', files);
    $.ajax({
      url: 'https://localhost/ajax/ajax_img.php?act=upload',
      dataType: "json",
      type: 'post',
      cache: false,
      data: fd,
      contentType: false,
      processData: false,
      success: function (response) {
        if (response != 0) {
          $('#img-loag-scrin').prepend(response.scrins);
          $('#textrea').val($('#textrea').val() + " " + response.textrea);
          $('#img_akb').remove();
        } else {
          alert('File not uploaded');
        }
      }
    });
  });
});

Kode html

<form method="post" action="" enctype="multipart/form-data" id="myform">
   <input type="file" id="file" name="file">
</form>
0
Akbarali 4 April 2020, 05:23