1. 程式人生 > 其它 >VUE 點選按鈕匯入file檔案

VUE 點選按鈕匯入file檔案

1:

2:

3:

程式碼:

<template>
  <div class="btn_box">
    <input type="file" accept=".xls" ref="filebox" id="fileinput" style="display: none;" @change="checkFileSure">
    <el-button @click='checkFile'>
      <svg class="icon icon-back" aria-hidden="true">
        <use xlink:href="
#icon-daoru1"></use> </svg>交付需求匯入 </el-button> </div> </template> <script> import { deliveryPlan_PlanImportNotice } from "../../../api/APSAPI/ScheduleManagement"; export default { name: "demo", data() { return { fileName: '', uploadForm:
new FormData(), } }, watch: { }, created() { }, methods: { // 匯入按鈕點選事件 checkFile() { document.querySelector('#fileinput').click() }, // 選擇的檔案 checkFileSure(val) { console.log(document.querySelector('#fileinput').files[0]);
this.fileName = document.querySelector('#fileinput').files[0].name; this.$forceUpdate(); //確認提示 this.$confirm('確認匯入"' + document.querySelector('#fileinput').files[0].name + '"嗎?', "警告", { confirmButtonText: "確定", cancelButtonText: "取消", type: "warning" }).then(function() { }).then(() => { console.log('匯入上傳資料'); this.uploadForm.append('file', document.querySelector('#fileinput').files[0]); //呼叫介面----------------------------------------------------- deliveryPlan_PlanImportNotice(this.uploadForm).then(response => { console.log('匯入返回結果') console.log(response) this.msgSuccess("匯入完成"); this.$refs.filebox.value = ''; this.uploadForm = new FormData() }).catch(error => { this.$refs.filebox.value = ''; this.uploadForm = new FormData() this.msgError('匯入失敗'); }); //end----------------------------------------------------- }).catch(() => { document.querySelector('#fileinput').value = ''; this.$refs.filebox.value = ''; this.uploadForm = new FormData() console.log(document.querySelector('#fileinput').files[0]); this.fileName = ""; }); }, } }; </script>