【问题标题】:Upload file does not accept the same file twice after i delete it上传文件在我删除后两次不接受同一个文件
【发布时间】:2022-10-21 21:15:37
【问题描述】:

我正在尝试使用此代码上传文件

onDrag(event:any) {
console.log(this.toUpload);
 if(this.toUpload.length >0){
   this.error = "Only one file at the time is accepted";
  }else{
    let fileName = event[0].name;
    let split = fileName.split(".");
    let ext = split[split.length - 1].toLowerCase();
    if(ext !="xlsx" && ext!="xls" ){
    this.error = "Only xls or xlsx files are supported";
  }else{
    if(event[0].size > 28000000){
      this.error = "the file is too big"
    }else{
      this.toUpload.push(event[0]);
      this.error = null;
    }
  }
}
console.log(this.toUpload);
}



<div class="dropzone my-3"
(click)="fileInput.click()" appDragdrop (onFileDropped)="onDrag($event)">
  <input hidden accept=".xls,.xlsx" type="file" #fileInput (change)="onDrag($any($event).target.files);">
  <img src="assets/img/dnd/ic-upload-file.svg" alt="" />
  <h3>Aggiungi Registro</h3>

斯福利亚...

我有两个问题

第一个不太重要的是拖放不起作用

我真正需要解决的一个问题是,如果我通过navigate files 选择一个文件,选择File.xlsx 并按下删除按钮,它会从事件[] 中正确删除,但我无法再次上传相同的文件.如果我更改文件它可以工作,只要我不删除它 当这种情况发生时,我什至无法到达第一个 console.log(),所以它基本上不会进入方法内部

当我刷新页面时,一切都会再次起作用

正确上传后,文件会发送到后端并正确处理...所以我认为这不是与文件相关的问题

我能做些什么?

【问题讨论】:

    标签: html angular typescript file-upload


    【解决方案1】:

    这个解决方案对我有用,从队列中删除一个元素后,您需要清除输入值。

    ts文件

     @ViewChild('fileInput', {static: false}) fileInput: ElementRef;
    
    
    
    deleteFromArray(index) {
    this.fileInput.nativeElement.value = null;
    ...
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2012-07-10
      • 2020-10-04
      • 1970-01-01
      • 2015-02-14
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多