vue項目中解決文件上傳 change事件只執行一次的問題
文件上傳第一次上傳一個文件后,再次上傳這個文件,無法執行change事件,
01) 解決辦法,借助v-if
02) 使用 Ant Design Vue 官方的 upload
demo:
<template> <div> <h3>這里是需求頁面</h3> <input id="fileUpload" type="file" name="fileUpload" accept=".xls,.xlsx,.pdf,.txt,.doc,.docx,.jpg,.gif,.png,.ppt,.pptx" multiple style="" @change="changesFile" v-if="isShowFile" /> <a-button ghost class="bts" type="primary" size="small" icon="plus" @click="addFiles">新增 </a-button> </div> </template> <script> import Vue from 'vue' import Antd, { message,Select } from 'ant-design-vue' //這是ant-design-vue import 'ant-design-vue/dist/antd.css' Vue.use(Antd); export default { components:{}, data() { return { isShowFile:true, //是否展示文件,選擇上傳 } }, methods: { // 新增文檔 addFiles(){ // $('#fileUpload').click(); document.getElementById("fileUpload").click(); }, changesFile(){ this.isShowFile = false; const formData = new FormData(); const fileObj=document.getElementById("fileUpload").files[0]; console.log(fileObj.size); if(fileObj!="undefined"){ this.$post("https://www.mocky.io/v2/5cc8019d300000980a055e76", formData).then(res => { this.isShowFile = true; console.log("eeeeee"); }); } }, } }; </script>