gpt4 book ai didi

javascript - VueJS在选择相同文件时重新读取文件输入

转载 作者:行者123 更新时间:2023-12-03 02:50:18 24 4
gpt4 key购买 nike

我想要实现的是选择一个文件并在新对话框中读取其内容。

我一直在工作,直到我尝试选择与之前加载的文件相同的文件,选择不同的文件效果很好。

我已尝试清除文件输入,这似乎有效,但它仍然没有再次选择该文件。

请参阅下面的示例,非常感谢任何帮助!

Codepen

new Vue({
el: "#app",
data: () => ({
importedData: null,
dialogImport: false
}),
watch: {
importedData: {
handler() {
this.checkData();
},
deep: true
}
},
methods: {
openFileExplorer() {
this.$refs.fileInputImport.value = "";
this.$refs.fileInputImport.click();
},
selectFile(e) {
const self = this;
const file = e.target.files[0];
let reader = new FileReader();
reader.onload = e => {
self.importedData = reader.result;
self.checkedData = true;
};
reader.readAsText(file);
},
checkData() {
// check the file
this.dialogImport = true;
}
}
});
<link href="https://unpkg.com/vuetify@0.17.4/dist/vuetify.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons" rel="stylesheet" />

<div id="app">
<v-app id="inspire">
<div class="text-xs-center">

<input ref="fileInputImport" type="file" name="fileInputImport" @change="selectFile">

<v-btn color="primary" @click.stop="openFileExplorer()">Choose file</v-btn>


<v-dialog v-model="dialogImport" fullscreen transition="dialog-bottom-transition" :overlay="false" scrollable>
<v-container class="ma-0 pa-0 white" style="max-width:100%">
<v-layout row wrap justify-left>

<v-card style="width:100%;">
<v-toolbar class="amber lighten-1 elevation-0">
<v-toolbar-title>Imported data</v-toolbar-title>
<v-spacer></v-spacer>
<div>
<v-flex xs12>
<v-container fluid>
<v-layout row align-center justify-center>

<v-flex>
<v-tooltip bottom close-delay="0">
<v-btn icon slot="activator" @click.native="dialogImport = false">
<v-icon>close</v-icon>
</v-btn>
<span>Close</span>
</v-tooltip>
</v-flex>

</v-layout>
</v-container>
</v-flex>
</div>
</v-toolbar>
<div>{{ importedData }}</div>
</v-card>
</v-layout>
</v-container>
</v-dialog>

</div>
</v-app>
</div>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify@0.17.4/dist/vuetify.min.js"></script>

最佳答案

我认为不需要watch。这是更新后的codepen

它能够读取文本并将其分配给importedData。但是当您选择相同的文件时,importedData具有相同的内容,watch没有尝试this.checkData()

关闭对话框时重置importedData,或者像我一样删除 watch 。

关于javascript - VueJS在选择相同文件时重新读取文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com