Vue項(xiàng)目導(dǎo)入導(dǎo)出文件功能以及導(dǎo)出文件后亂碼問(wèn)題及解決
vue項(xiàng)目導(dǎo)入導(dǎo)出功能
1.導(dǎo)出
導(dǎo)出功能其實(shí)很簡(jiǎn)單,只需要調(diào)用后端的接口即可,不過(guò)有一些需要注意的地方,具體代碼如下所示:
這是導(dǎo)出按鈕,定義導(dǎo)出的點(diǎn)擊事件:
<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("請(qǐng)先選擇需要導(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)
})
}
在這里需要注意的是:
點(diǎn)擊導(dǎo)出按鈕,接口跑通后,文件會(huì)成功導(dǎo)出,不過(guò)打開(kāi)文件會(huì)出現(xiàn)亂碼,這個(gè)時(shí)候我們需要配置下接口,具體代碼如下所示:
// 導(dǎo)出月度薪資
export const apiDownExcel = (params: object) => {
return request({
url: "/api/slaughter_smart/mySalaryMonthResult/exportExcel",
method: "post",
data: params,
responseType: "blob",
})
}
需要設(shè)置 ** responseType: “blob” ** 即可正常打開(kāi)該文件。
2.導(dǎo)入
導(dǎo)入功能也不難,ant-design-vue已經(jīng)封裝了上傳組件,我們按照upload上傳組件開(kāi)發(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是上傳文件請(qǐng)求的路徑,headers里面設(shè)置了token,accept表示接受文件的格式,multiple表示是否上傳多個(gè)文件,showUploadList表示是否展示uploadList,@change則表示上傳文件改變時(shí)的狀態(tài),上傳文件有三種狀態(tài),分別是uploading、done以及error。
使用action作為上傳文件的接口路徑,通常是沒(méi)有攜帶token的,所以需要在請(qǐng)求頭里自定義帶上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()
}
}
當(dāng)然,還有其他的情況,還可以使用自定義上傳的方式,通過(guò)customRequest覆蓋默認(rèn)上傳的方式
3.另一種方法實(shí)現(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">(注:請(qǐng)按照模板進(jìn)行設(shè)計(jì),如與模板不符將無(wú)法錄入)</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">確認(rèn)</a-button>
<a-button
@click="cancelModal"
>取消</a-button
>
</div>
</a-modal>
該彈框打開(kāi)后效果如下圖所示:

js部分:
// 自定義導(dǎo)入
const customRequestChange = (options: any) => {
modalDate.fileList.push(options.file)
}
// 點(diǎn)擊確認(rèn)上傳文件
const imporSaveClick = () => {
if (modalDate.fileList.length == 0) {
message.warning("請(qǐng)選擇上傳的文件")
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)
}
})
}
該寫(xiě)法只能上傳一個(gè)文件!??!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue不操作dom實(shí)現(xiàn)圖片輪播的示例代碼
這篇文章主要介紹了vue不操作dom實(shí)現(xiàn)圖片輪播的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue運(yùn)行報(bào)錯(cuò)cache-loader的解決步驟
最近運(yùn)行vue項(xiàng)目的時(shí)候報(bào)錯(cuò)了,通過(guò)查找相關(guān)資料最終解決,下面這篇文章主要給大家介紹了關(guān)于vue運(yùn)行報(bào)錯(cuò)cache-loader的解決步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式
這篇文章主要介紹了Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue組件props不同數(shù)據(jù)類(lèi)型傳參的默認(rèn)值問(wèn)題
這篇文章主要介紹了vue組件props不同數(shù)據(jù)類(lèi)型傳參的默認(rèn)值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法
這篇文章主要為大家介紹了想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Vue實(shí)現(xiàn) 點(diǎn)擊顯示再點(diǎn)擊隱藏效果(點(diǎn)擊頁(yè)面空白區(qū)域也隱藏效果)
這篇文章主要介紹了Vue實(shí)現(xiàn) 點(diǎn)擊顯示 再點(diǎn)擊隱藏 點(diǎn)擊頁(yè)面空白區(qū)域也隱藏效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例
下面小編就為大家分享一篇Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue-cli3使用mock數(shù)據(jù)的方法分析
這篇文章主要介紹了vue-cli3使用mock數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了vue-cli3使用mock數(shù)據(jù)的相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
京東 Vue3 組件庫(kù)支持小程序開(kāi)發(fā)的詳細(xì)流程
這篇文章主要介紹了京東 Vue3 組件庫(kù)支持小程序開(kāi)發(fā)的詳細(xì)流程,通過(guò)引入NutUI,即可在項(xiàng)目中使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-06-06

