前端根据pdf连接点击下载pdf而不是直接打开

参考地址: https://www.cnblogs.com/jackson-yqj/p/11321275.html

/**
   * 文件链接转文件流下载--主要针对pdf 解决谷歌浏览器a标签下载pdf直接打开的问题
   * @param url  :文件链接
   * @param fileName  :文件名;
   * @param type  :文件类型;
   */
  function fileLinkToStreamDownload(url, fileName, type) {
    let reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
    if (!reg.test(url)) {
     throw new Error("传入参数不合法,不是标准的文件链接");
   } else {
     let xhr = new XMLHttpRequest();
     xhr.open('get', url, true);
     xhr.setRequestHeader('Content-Type', `application/${type}`);
     xhr.responseType = "blob";
     xhr.onload = function () {
       if (this.status == 200) {
         //接受二进制文件流
         var blob = this.response;
         downloadExportFile(blob, fileName, type)
       }
     }
     xhr.send();
   }
  }
/**
 *下载导出文件
 * @param blob  :返回数据的blob对象或链接
 * @param tagFileName  :下载后文件名标记
 * @param fileType  :文件类 word(docx) excel(xlsx) ppt等
 */
function downloadExportFile(blob, tagFileName, fileType) {
  let downloadElement = document.createElement('a');
  let href = blob;
  if (typeof blob == 'string') {
    downloadElement.target = '_blank';
  } else {
    href = window.URL.createObjectURL(blob); //创建下载的链接
  }
  downloadElement.href = href;
  downloadElement.download = tagFileName + moment(new Date().getTime()).format('YYYYMMDDhhmmss') + '.' + fileType; //下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
  if (typeof blob != 'string') {
    window.URL.revokeObjectURL(href); //释放掉blob对象
  }

}
/**
 * base64对象转文件对象
 * @param urlData  :数据的base64对象
 * @param type  :类型 image/png;
 * @returns {Blob}:Blob文件对象
 */
function base64ToBlob(urlData, type) {
  let arr = urlData.split(',');
  let array = arr[0].match(/:(.*?);/)
  let mime = (array && array.length > 1 ? array[1] : type) || type;
  // 去掉url的头,并转化为byte
  let bytes = window.atob(arr[1]);
  // 处理异常,将ascii码小于0的转换为大于0
  let ab = new ArrayBuffer(bytes.length);
  // 生成视图(直接针对内存):8位无符号整数,长度1个字节
  let ia = new Uint8Array(ab);
  for (let i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i);
  }
  return new Blob([ab], {
    type: mime
  });
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 06:22:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 06:22:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 06:22:01       87 阅读
  4. Python语言-面向对象

    2024-03-23 06:22:01       96 阅读

热门阅读

  1. Hive常用函数 之 数值处理

    2024-03-23 06:22:01       44 阅读
  2. android AMS的面试题目

    2024-03-23 06:22:01       45 阅读
  3. Flink 架构深度解析

    2024-03-23 06:22:01       41 阅读
  4. webpack proxy工作原理?为什么能解决跨域?

    2024-03-23 06:22:01       48 阅读
  5. 新概念英语1:Lesson7内容详解

    2024-03-23 06:22:01       42 阅读
  6. vim | 安装 vimspector 调试神器

    2024-03-23 06:22:01       41 阅读
  7. C语言-整数与浮点数:内存存储的差异

    2024-03-23 06:22:01       43 阅读
  8. C#面:什么是不可变字符串

    2024-03-23 06:22:01       35 阅读
  9. GO 语言基础学习记录

    2024-03-23 06:22:01       36 阅读