vue使用file-saver插件保存各種格式文件方式
使用file-saver插件保存各種格式文件方式
首先下載插件file-saver
npm install file-saver
再封裝組件
import FileSaver from "file-saver";
export default class fileSave {
/**
* 導(dǎo)出Excel文件
* @param {*} res 文件流
* @param {*} name 文件名
*/
static getExcel(res, name) {
console.log(res, name)
let blob = new Blob([res], {
type: "application/vnd.ms-excel"
});
FileSaver.saveAs(blob, name + ".xlsx");
}
/**
* 導(dǎo)出CSV文件
* @param {*} res 文件流
* @param {*} name 文件名
*/
static getCsv(res, name) {
let blob = new Blob([res], {
type: "application/vnd.ms-excel"
});
FileSaver.saveAs(blob, name + ".csv");
}
/**
* 導(dǎo)出圖片1
* @param {*} url 圖片地址
* @param {*} name 文件名
*/
static getImgURLs(url, name) {
let last = url.substring(url.lastIndexOf("."), url.length);
FileSaver.saveAs(url, `${name}${last}`);
}
/**
* 導(dǎo)出圖片2
* @param {*} res 文件流
* @param {*} name 文件名
*/
static downLoadImg(res, filename) {
let blob = new Blob([res], {
type: "image/jpeg"
});
FileSaver.saveAs(blob, `${filename}.jpg`);
}
}vue導(dǎo)出文件(file-saver,vue2,vue3)
安裝插件
npm install file-saver --save // 如使用ts,安裝file-saver的typeScript類(lèi)型定義 npm install @types/file-saver --save-dev
導(dǎo)出的格式類(lèi)型參考
| 文件后綴 | Type |
|---|---|
| application/pdf | |
| .doc | application/msword |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .xls | application/vnd.ms-excel |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .ppt | application/vnd.ms-powerpoint |
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
基本使用
代碼如下:
import { saveAs } from 'file-saver'導(dǎo)出(下載文件):
//??注意:需要配置你需要導(dǎo)出的文件類(lèi)型
const blob = new Blob(['文件內(nèi)容'],
{ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'})
saveAs(blob,'導(dǎo)出的文件名字')案例
import axios from 'axios'
import { saveAs } from 'file-saver' axios({
method: 'get',
url: url,
responseType: 'blob',
//注入token流,需要添加不需要?jiǎng)t無(wú)需添加。
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-
officedocument.wordprocessingml.document' })
saveAs(blob, name)
} else {
console.log('接口報(bào)錯(cuò)')
}
})總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli項(xiàng)目中怎么使用mock數(shù)據(jù)
本篇文章主要介紹了vue-cli項(xiàng)目中怎么使用mock數(shù)據(jù) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
VueJs監(jiān)聽(tīng)window.resize方法示例
本篇文章主要介紹了VueJs監(jiān)聽(tīng)window.resize方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
vue選項(xiàng)卡Tabs組件實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了vue選項(xiàng)卡Tabs組件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue3使用vuex實(shí)現(xiàn)頁(yè)面實(shí)時(shí)更新數(shù)據(jù)實(shí)例教程(setup)
在前端開(kāi)發(fā)中往往會(huì)遇到頁(yè)面需要實(shí)時(shí)刷新數(shù)據(jù)的情況,給用戶最新的數(shù)據(jù)展示,這篇文章主要給大家介紹了關(guān)于vue3使用vuex實(shí)現(xiàn)頁(yè)面實(shí)時(shí)更新數(shù)據(jù)(setup)的相關(guān)資料,需要的朋友可以參考下2022-09-09
vue日常開(kāi)發(fā)基礎(chǔ)Axios網(wǎng)絡(luò)庫(kù)封裝
這篇文章主要為大家介紹了vue日常開(kāi)發(fā)基礎(chǔ)Axios網(wǎng)絡(luò)庫(kù)封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue的.vue文件是怎么run起來(lái)的(vue-loader)
通過(guò)vue-loader,解析.vue文件,在webpack解析,拆解vue組件 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下2018-12-12
解決vite+vue3項(xiàng)目打包后圖片不顯示或者請(qǐng)求路徑多了一個(gè)undefined問(wèn)題
這篇文章主要介紹了解決vite+vue3項(xiàng)目打包后圖片不顯示或者請(qǐng)求路徑多了一個(gè)undefined問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
webpack項(xiàng)目調(diào)試以及獨(dú)立打包配置文件的方法
下面小編就為大家分享一篇webpack項(xiàng)目調(diào)試以及獨(dú)立打包配置文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
配置vite.confgi.ts無(wú)法使用require問(wèn)題以及解決
這篇文章主要介紹了配置vite.confgi.ts無(wú)法使用require問(wèn)題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

