html - 如何在html和typescript中连续两次导入同一个文件?
问题描述
我在 html 文件中有一个输入元素,如下所示:
<input
type="file"
(change)="receiveFile($event)"
id="inputFileButton"
hidden
/>
它正在等待用户导入文件。单击按钮时,我调用uploadFile() 方法,该方法在调用receiveFile($event) 的输入元素上发出事件。像这样:
receiveFile($event) {
const fileFromHtml = $event.target.files[0];
const importedFile: FormData = new FormData();
importedFile.append('positions', fileFromHtml);
this.store.dispatch({
type: '[Position] Import Positions',
importFile: importedFile,
positionSetId: this.positionSetId,
accountSetId: this.accountSetId,
});
}
uploadFile() {
this.testvar = !this.testvar;
const element: HTMLElement = document.getElementById(
'inputFileButton',
) as HTMLElement;
element.click();
}
除了我想连续两次导入同一个文件的情况外,它工作正常。第二次它根本不调用receiveFile($event)。
解决方案
您的 receiveFile() 函数仅在更改事件上运行。如果输入相同的文件,没有任何改变,所以它不会运行。
为了克服这个问题,您可以尝试清除您的输入
document.getElementById("inputFileButton").value = ''
推荐阅读
- javascript - 如果在 express 中使用相同的路由,如何防止重定向到再次请求
- angular - 在 Angular Google Maps 数据层 LayerClick 事件中获取功能 ID
- javascript - 在 Chrome 扩展程序中创建具有变量名的新类
- azure - 在不同项目中链接 Azure DevOps Repos
- arrays - 用于渲染 url `${item}` 的函数 array.find() 反应原生不起作用
- rpm - yum 在实际的“必需”包之前安装“提供”包
- node.js - 节点中的线程与其他编程语言
- reactjs - React 钩子和条件
- c# - 硬代码大十六进制值在 C# 中引发错误
- javascript - 域地址的 CORS 问题,但使用直接 IP 地址和 localhost 可以正常工作