Vue項目導(dǎo)入導(dǎo)出文件功能以及導(dǎo)出文件后亂碼問題及解決
vue項目導(dǎo)入導(dǎo)出功能
1.導(dǎo)出
導(dǎo)出功能其實很簡單,只需要調(diào)用后端的接口即可,不過有一些需要注意的地方,具體代碼如下所示:
這是導(dǎo)出按鈕,定義導(dǎo)出的點擊事件:
<a-button type="primary" @click="downExcel"> <template #icon> <UploadOutlined /> </template> 導(dǎo)出 </a-button>
js部分,調(diào)用接口,導(dǎo)出文件:
// 導(dǎo)出excel const downExcel = () => { if (state.selectedRowKeys.length == 0) { Message.warning("請先選擇需要導(dǎo)出的數(shù)據(jù)") return } // const date = moment(new Date()).format("YYYY-MM") const params = { exportUserIds: state.selectedRowKeys } const hideLoading = message.loading("文件導(dǎo)出中...", 0) apiDownExcel(params) .then((res: any) => { if (res.status == 200) { const system = window.navigator.platform == "Win32" ? "window" : "mac" const link = document.createElement("a") const blob = new Blob([res.data], { type: "application/vnd.ms-excel" }) link.href = URL.createObjectURL(blob) link.download = `月度薪資.${system == "mac" ? "xlw" : "xls"}` //下載的文件名 document.body.appendChild(link) link.click() document.body.removeChild(link) } }) .finally(() => { setTimeout(hideLoading, 0) }) }
在這里需要注意的是:
點擊導(dǎo)出按鈕,接口跑通后,文件會成功導(dǎo)出,不過打開文件會出現(xiàn)亂碼,這個時候我們需要配置下接口,具體代碼如下所示:
// 導(dǎo)出月度薪資 export const apiDownExcel = (params: object) => { return request({ url: "/api/slaughter_smart/mySalaryMonthResult/exportExcel", method: "post", data: params, responseType: "blob", }) }
需要設(shè)置 ** responseType: “blob” ** 即可正常打開該文件。
2.導(dǎo)入
導(dǎo)入功能也不難,ant-design-vue已經(jīng)封裝了上傳組件,我們按照upload上傳組件開發(fā)即可,具體代碼如下所示:
<a-upload name="file" action="/api/slaughter_smart/mySalaryMonthResult/importExcel" :headers="{ Authorization: token, 'Muyuan-Auth': token }" accept=".xls,.xlsx" :multiple="false" :showUploadList="false" @change="uploadChangeHandle > <a-button type="primary"> <template #icon> <DownloadOutlined /> </template> 導(dǎo)入 </a-button> </a-upload>
解析:
action是上傳文件請求的路徑,headers里面設(shè)置了token,accept表示接受文件的格式,multiple表示是否上傳多個文件,showUploadList表示是否展示uploadList,@change則表示上傳文件改變時的狀態(tài),上傳文件有三種狀態(tài),分別是uploading、done以及error。
使用action作為上傳文件的接口路徑,通常是沒有攜帶token的,所以需要在請求頭里自定義帶上token。
文件上傳后js部分代碼:
// 文件狀態(tài) -- 上傳文件后更新列表 const uploadChangeHandle = (data: any) => { if (data.file.status == "done") { Message.success("文件導(dǎo)入成功") getList() } if (data.file.status == "error") { Message.error(data.file.response.message ? data.file.response.message : "文件導(dǎo)入失敗") getList() } }
當然,還有其他的情況,還可以使用自定義上傳的方式,通過customRequest覆蓋默認上傳的方式
3.另一種方法實現(xiàn)文件上傳
接口部分:
// 導(dǎo)入月度薪資 export const apiImportExcel = (params: any) => { return request({ url: '/api/slaughter_smart/mySalaryMonthResult/importExcel', method: 'post', data: params, headers: { 'Content-Type': 'multipart/form-data' } }) }
html部分:
<!-- 導(dǎo)入模板彈框 --> <a-modal title="導(dǎo)入" :visible="modalDate.importVisivle" @cancel="cancelModal" :maskClosable="false" :footer="null" :width="540"> <div> <a-button type="primary" ghost @click="downloadTemplateClick"> <template #icon> <DownloadOutlined /> </template> 模板下載 </a-button> <div style="font-size: 14px;color: #FF4122;font-weight: 400;margin-top: 5px">(注:請按照模板進行設(shè)計,如與模板不符將無法錄入)</div> </div> <div style="margin-top: 30px"> <a-upload :showUploadList="false" accept=".xls,.xlsx,xlw" :customRequest="customRequestChange"> <a-button type="primary" ghost v-if="!modalDate.fileList.length"> <UploadOutlined /> 上傳文件 </a-button> <a-button type="primary" ghost disabled v-else> <UploadOutlined /> 上傳文件 </a-button> </a-upload> <div class="martp_15"> <div class="dis_flex flex_y_center" v-for="(item, index) in modalDate.fileList" :key="index"> <i class="iconfont icon-paperclip marrt_10" style="font-size: 14px;color: #0194FE"></i> <span style="color: #0194FE">{{ item.name }}</span> <i class="iconfont icon-icon_close_remove" style="font-size: 20px;margin-left: 15px" @click.stop="deleteFileClick(index)"></i> </div> </div> </div> <div class="text_right" style="margin-top: 20px"> <a-button class="marrt_15" type="primary" v-prevent-click @click="imporSaveClick">確認</a-button> <a-button @click="cancelModal" >取消</a-button > </div> </a-modal>
該彈框打開后效果如下圖所示:
js部分:
// 自定義導(dǎo)入 const customRequestChange = (options: any) => { modalDate.fileList.push(options.file) } // 點擊確認上傳文件 const imporSaveClick = () => { if (modalDate.fileList.length == 0) { message.warning("請選擇上傳的文件") return false } const params = new FormData() modalDate.fileList.forEach((item: any)=> { params.append("file", item) }) apiImportExcel(params).then((res: any)=>{ if (res.data.status == 200 && res.data.rel) { getList() message.success("上傳成功!") modalDate.importVisivle = false modalDate.fileList = [] }else { message.error(res.data.message) } }) }
該寫法只能上傳一個文件?。?!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue系列之requireJs中引入vue-router的方法
這篇文章主要介紹了vue系列之requireJs中引入vue-router的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07vue3?reactive響應(yīng)式依賴收集派發(fā)更新原理解析
這篇文章主要為大家介紹了vue3響應(yīng)式reactive依賴收集派發(fā)更新原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03原生JS?Intersection?Observer?API實現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07Nuxt3項目搭建過程(Nuxt3+element-plus+scss詳細步驟)
這篇文章主要介紹了Nuxt3項目搭建(Nuxt3+element-plus+scss詳細步驟),本次記錄一次使用Nuxt3搭建前端項目的過程,內(nèi)容包含Nuxt3的安裝,基于Vite腳手架(默認)構(gòu)建的vue3項目,element-plus的安裝配置,scss的安裝,目錄結(jié)構(gòu)的創(chuàng)建和解釋,需要的朋友可以參考下2022-12-12vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式,文中通過介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02