我正在用VS2012和Javascript开发一个地铁应用程序

我想重置我的文件输入的内容:

<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />

我该怎么做呢?


当前回答

如果你有以下情况:

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

然后就这样做:

$("#fileControl").val('');

重置文件控件。

其他回答

另一种解决方案(不选择HTML DOM元素)

如果你在输入中添加了'change'事件监听器,那么在javascript代码中你可以调用(对于某些特定的条件):

event.target.value = '';

例如在HTML中:

<input type="file" onChange="onChangeFunction(event)">

在javascript中:

onChangeFunction(event) {
  let fileList = event.target.files;
  let file = fileList[0];
  let extension = file.name.match(/(?<=\.)\w+$/g)[0].toLowerCase(); // assuming that this file has any extension

  if (extension === 'jpg') {
    alert('Good file extension!');
  }
  else {
    event.target.value = '';
    alert('Wrong file extension! File input is cleared.');
  }

我在这里错过了另一个解决方案(2021年): 我使用FileList与文件输入交互。

因为没有办法创建FileList对象,所以我使用DataTransfer,它带来了干净的FilleList。

我用:

  file_input.files=new DataTransfer().files  

在我的例子中,这非常适合,因为我只通过FileList与封装的文件输入元素进行交互。

你需要将<input type = " file " >包装成<form>标签,然后你可以通过重置你的表单来重置输入:

onClick="this.form.reset()"

我在ng2-file-upload中遇到过angular的问题。如果你正在寻找angular的解决方案,请参考下面的代码

HTML:

<输入type=“文件”/>

组件

import {Component, OnInit, ElementRef, ViewChild} from @angular/core;

@ViewChild (activeFrameinputFile) InputFrameVariable: ElementRef;

this.frameUploader.onSuccessItem =(项目,响应,状态,头)=> {

`this.`**InputFrameVariable**`.nativeElement.value = '';`

};

getElementById (uploadCaptureInputFile”)的文件。价值;