The front end of the vue project implements pdf file download

Obtain the URL of the pdf from the interface, and realize the download function in the foreground

Idea: Use the get request to obtain a binary stream, then convert it to a Blob, then create a DOMString through window.URL.createObjectURL(), and assign it to the href of the a tag. The specific implementation is as follows: Step 1: Get requests the pdf path
,
because The blob requires a binary stream

/*
    url: "http://124.127.246.224:9000/images/2022/10/17/c24f8fd7d53.pdf"
    options: {title:"",fileType:"application/pdf"}
    ele: a标签
*/
let getFile = (url, options, ele) => {
    
    
    axios.get(url,{
    
     responseType: 'blob' }).then(res => download(res, options, ele))
}

Step 2: Convert the returned data to Blob

let download = (res, options, ele) => {
    
    
    let blob = new Blob([res.data], {
    
     type: options.fileType ? options.fileType : "application/octet-binary" });
    let href = window.URL.createObjectURL(blob);
    downloadBlob(href, options, ele);
}

Step 3: Execute the download
Pitfall here: If you do not add ele.click(), you need to click again to download the file. If you add click self-call, it will download twice, so use flag to record

let flag = 1;
let downloadBlob = (blobUrl, options, ele) => {
    
    
    ele.href = blobUrl;
    ele.download = options.title;
    flag++;
    if ((flag % 2) == 0) {
    
    
        ele.click();
        window.URL.revokeObjectURL(blobUrl);//释放
    }
}

Step 4: Call in the vue file

 <el-button type="primary"  @click="download(ele, index)">下载
     <a :id="'d' + index"></a>
 </el-button>

import {
    
    getFile} from "@/utils/download"

download(data, index) {
    
    
  let arr = data.url.split(".");
  let options = {
    
    
    title: data.title,
    fileType: "application/"+arr[arr.length-1]
  };
  let link = document.getElementById("d" + index);
  getFile(data.url,options,link)
},

The complete js file content is as follows:

//download.js
import axios from "axios"  //需要单独引入,因为当前.js文件不会被vue管理
axios.defaults.headers['Cache-Control'] = 'no-cache'
/*
    url: "http://124.127.246.224:9000/images/2022/10/17/c24f8fd7d53.pdf"
    options: {title:"",fileType:"application/pdf"}
    ele: a标签
*/
let getFile = (url, options, ele) => {
    
    
    axios.get(url, {
    
     responseType: 'blob' }).then(res => download(res, options, ele))
}

let download = (res, options, ele) => {
    
    
    let blob = new Blob([res.data], {
    
     type: options.fileType ? options.fileType : "application/octet-binary" });
    let href = window.URL.createObjectURL(blob);
    downloadBlob(href, options, ele);
}

let flag = 1;
let downloadBlob = (blobUrl, options, ele) => {
    
    
    ele.href = blobUrl;
    ele.download = options.title;
    flag++;
    if ((flag % 2) == 0) {
    
    
        ele.click();
        window.URL.revokeObjectURL(blobUrl);
    }
}

export {
    
    
    getFile,
    download,
    downloadBlob
}

If there is a better way of writing, corrections are very welcome, thank you~

Guess you like

Origin blog.csdn.net/qq_44415875/article/details/127756706