admin管理员组

文章数量:1545271

  • 经常出现点击pdf文件直接跳转到预览,以下是实现不跳转,直接下载文件的方法
  • 使用axios之前请求pdf路径,转换成流文件,然后可以直接进行进行下载,就不会直接打开pdf文件了
//使用axios直接请求pdf完整路径
axios({
   method: 'get',
   url: 'pdf文件.pdf',
   responseType: 'blob'
 }).then((res) => {
    this.downloadFile(res.data, "文件", ".pdf");
  }) 

/**
 * 
 * @param {*} obj 文件流
 * @param {*} name 文件名
 * @param {*} suffix 后缀
 */
downloadFile(obj, name, suffix) {
  const url = window.URL.createObjectURL(new Blob([obj]));
  const link = document.createElement("a");
  link.style.display = "none";
  link.href = url;
  const fileName = parseTime(new Date()) + "-" + name + "." + suffix;
  link.setAttribute("download", fileName);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

本文标签: 而在文件器里PDF