vue使用file-saver插件保存各種格式文件方式
更新時(shí)間:2024年07月01日 14:31:38 作者:Mr__proto__
這篇文章主要介紹了vue使用file-saver插件保存各種格式文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
使用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類型定義 npm install @types/file-saver --save-dev
導(dǎo)出的格式類型參考
文件后綴 | 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)出的文件類型 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)文章
前端實(shí)現(xiàn)簡(jiǎn)單的sse封裝方式(React hook Vue3)
這篇文章主要介紹了前端實(shí)現(xiàn)簡(jiǎn)單的sse封裝方式(React hook Vue3),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue注冊(cè)模塊與登錄狀態(tài)的持久化實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Vue注冊(cè)模塊與登錄狀態(tài)的持久化實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Vue3?Transition組件給頁(yè)面切換并加上動(dòng)畫效果
這篇文章主要給大家介紹了關(guān)于Vue3?Transition組件給頁(yè)面切換并加上動(dòng)畫效果的相關(guān)資料,vue的過(guò)渡動(dòng)畫主要是transition標(biāo)簽的使用,配合css動(dòng)畫實(shí)現(xiàn)的,需要的朋友可以參考下2023-06-06用vue的雙向綁定簡(jiǎn)單實(shí)現(xiàn)一個(gè)todo-list的示例代碼
本篇文章主要介紹了用vue的雙向綁定簡(jiǎn)單實(shí)現(xiàn)一個(gè)todo的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08vue3+ts+Vuex中使用websocket協(xié)議方式
這篇文章主要介紹了vue3+ts+Vuex中使用websocket協(xié)議方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Nuxt的動(dòng)態(tài)路由和參數(shù)校驗(yàn)操作
這篇文章主要介紹了Nuxt的動(dòng)態(tài)路由和參數(shù)校驗(yàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11