最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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類(lèi)型定義
npm install @types/file-saver --save-dev

導(dǎo)出的格式類(lèi)型參考

文件后綴Type
.pdfapplication/pdf
.docapplication/msword
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
.xlsapplication/vnd.ms-excel
.xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.pptapplication/vnd.ms-powerpoint
.pptxapplication/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)文章

最新評(píng)論

松阳县| 视频| 博湖县| 集贤县| 洛扎县| 大兴区| 龙川县| 平山县| 濮阳市| 桂东县| 沙坪坝区| 青川县| 东明县| 简阳市| 越西县| 望都县| 含山县| 浑源县| 绍兴县| 崇左市| 双辽市| 河池市| 广德县| 大竹县| 徐州市| 石门县| 中阳县| 寿宁县| 屏东县| 博兴县| 城口县| 固始县| 江永县| 五常市| 绥芬河市| 梅州市| 富源县| 荔浦县| 卓尼县| 元江| 积石山|