javascript - 如何在不使用 vue/javascript 中的 event.target 的情况下显示上传的 zip 文件的名称?
问题描述
我有一个输入,它接受文件类型并接受特定的 zip 文件。一旦动态上传到相关的输入标签中,我想显示 zip 文件的名称。现在我可以使用一种方法检索文件的名称,但它需要event
从输入中获取。当我收到此错误时,尝试动态呈现文件名时失败:
app.js:164890 [Vue warn]: Error in render: "TypeError: Cannot read property 'target' of undefined"
如果我e
在调用时通过了,{{fileName(e)}}
那么我会收到此错误
[Vue warn]: Property or method "e" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property
这是我的 HTML 标签:
<input
id="zip"
name="zip"
type="file"
accept="application/zip"
required>
<label for="zip">{{fileName()}}</label>
Vue方法
fileName(e) {
return e.target.files[0].name ? return e.target.files[0].name : 'Choose File';
}
解决方案
change
您可以通过事件处理程序监听输入事件,并在用户每次选择文件时检索文件名。
new Vue({
el: "#app",
data: {
fileName: "Choose File",
},
methods: {
fileSelected: function(e){
this.fileName = e.target.files[0].name;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<input
id="zip"
name="zip"
type="file"
accept="application/zip"
required
v-on:change="fileSelected">
<label for="zip">{{fileName}}</label>
</div>
推荐阅读
- java - 不同机器上的单个进程与单台机器上的多线程,核心等于单 CPU 机器的数量
- date - 日期到 Excel 时间戳
- python - 想要覆盖或操作 PDF 的页面大小
- sql-server-2012 - 是否可以对镜像数据库执行查询?(MS SQL 服务器 2012)
- python - (令人惊讶的挑战?)Numpy Vectorization
- javascript - 使用预定道具渲染条件组件
- c# - COBOL 和 C# 之间的数据通信
- python - 无法使用 AWS Lambda 写入数据库
- java - 线程“主”java.lang.NoSuchMethodError 中的异常:java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer
- c# - httpClient 不响应