download.js (实例1)下载

import axios from 'axios'
import { getToken } from '@/utils/util'
// 文件流转blob对象下载
var baseURL = ''
if (process.env.NODE_ENV == 'production') {
    baseURL = process.env.VUE_APP_API_URL
} else if (process.env.NODE_ENV == 'test') {
    baseURL = process.env.VUE_APP_API_URL
}
export function downloadFile( fileName,appendixId) {

    console.log(appendixId);
    var url = baseURL + `/api/appendix/download/${appendixId}`
    let index = fileName.lastIndexOf(".")
        let strType = fileName.substring(index+1, fileName.length);
        let endName = fileName.substring(0, index);
        console.log(strType)
        console.log(endName)
    axios({
        method: 'get',
        url: url,   
        responseType: 'blob',
        headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then(res => {
        // let blob = new Blob([res.data], {type: `application/txt;charset=utf-8`});
        let blob = res.data;
        console.log(blob);
        // 获取heads中的filename文件名
        let downloadElement = document.createElement('a');
        // 创建下载的链接
        let href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        // 下载后文件名
        downloadElement.download = fileName;
        document.body.appendChild(downloadElement);
        // 点击下载
        downloadElement.click();
        // 下载完成移除元素
        document.body.removeChild(downloadElement);
        // 释放掉blob对象
        window.URL.revokeObjectURL(href);
    })

  
}
export function fileReview( fileName,appendixId,_this) {

    console.log(appendixId);

    // 设置响应类型
    let responseType =
        fileName.split(".")[fileName.split(".").length - 1] == "pdf"
            ? "arraybuffer"
            : "blob";
    let type = fileName.split(".")[fileName.split(".").length - 1]; // 文件类型
    if (
        !(type == "jpg" || type == "pdf" || type == "png" || type == "jpeg")
    ) {
        _this.$message.warning("请下载后预览");
    } else {

        var url = baseURL + `/api/appendix/download/${appendixId}`
        let index = fileName.lastIndexOf(".")
            let strType = fileName.substring(index+1, fileName.length);
            let endName = fileName.substring(0, index);
            console.log(strType)
            console.log(endName)
        axios({
            method: 'get',
            url: url,   
            responseType: 'blob',
            headers: { 'Authorization': 'Bearer ' + getToken() }
        }).then(res => {
            // let blob = new Blob([res.data], {type: `application/txt;charset=utf-8`});
            console.log(res)
            let blob = res.data;
                  // 图片预览
                  if (responseType == "blob") {
                    _this.$showDialogImg( window.URL.createObjectURL(new Blob([blob])))
                }
                // pdf预览
                if (responseType == "arraybuffer") {
                    window.open(
                        window.URL.createObjectURL(
                            new Blob([blob], { type: "application/pdf" })
                        )
                    );
                }
       
    
        })
    }
  
}

 

posted @ 2022-11-04 11:31  ThisCall  阅读(202)  评论(0编辑  收藏  举报