为了账号安全,请及时绑定邮箱和手机立即绑定

将文件上传到 html 输入时如何自动加载?

将文件上传到 html 输入时如何自动加载?

繁星点点滴滴 2023-09-18 15:58:41
当我将文件放入 html 输入中时,我想通过 ajax post 上传。(无需点击)它在我单击当前代码中的启动按钮后起作用。我的代码    $(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');        }      }    });  });});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>当 html 插入到输入文件中时,无需单击按钮,就应该加载该文件。加载图像后,图像必须从输入中消失
查看完整描述

1 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

我用过改变。


$(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');

        }

      }

    });

  });

});

html代码


<form method="post" action="" enctype="multipart/form-data" id="myform">

   <input type="file" id="file" name="file">

</form>


查看完整回答
反对 回复 2023-09-18
  • 1 回答
  • 0 关注
  • 47 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信