input:file onchange事件,点击相同文件无法触发

发布时间 2023-12-08 09:10:06作者: 喵师傅

感谢:https://www.cnblogs.com/slikes/p/12145140.html

多次选择相同文件时,输入框没有回显文件名

通过钩子函数初始化文件报异常:
Failed to set the 'files' property on 'HTMLInputElement': Failed to convert value to 'FileList'.

解决办法

// 清空文件上传控件
// 不能直接用js修改input type=file的value,但可以通过form的reset()清空它的值
// 解决:将input type=file放进一个临时form,清空value,再将它移到原来位置
 this.emptyFileUpload($('#impotFiles'));
 
// 以下为methods中的方法
 emptyFileUpload(selector) {
      var fi;
      var sourceParent;
      if (selector) {
        fi = $(selector);
        sourceParent = fi.parent();
      }
      else {
          return;
      }
      $("<form id='tempForm'></form>").appendTo(document.body);
      var tempForm = $("#tempForm");
      tempForm.append(fi);
      tempForm.get(0).reset();
      sourceParent.append(fi);
      tempForm.remove();
  }