1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Test</title>
    </head>
    <body>
        <script>
            /**
 * @deprecated 下载文件
 * @param {string} url 
 * @param {string} filename
 */
handleFileDownload = (url, filename) => {
  // 创建 a 标签
  let a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click();
}

/**
 * @deprecated 处理 pdf url,使其不在浏览器打开
 * @param {string} url
 */
handlePdfLink = (url, filename) => {
  fetch(url, {
    method: 'get',
    responseType: 'arraybuffer',
  })
    .then(function (res) {
      if (res.status !== 200) {
        return res.json()
      }
      return res.arrayBuffer()
    })
    .then((blobRes) => {
    	// 生成 Blob 对象,设置 type 等信息
      const e = new Blob([blobRes], {
        type: 'application/octet-stream',
        'Content-Disposition':'attachment'
      })
      // 将 Blob 对象转为 url
      const link = window.URL.createObjectURL(e)
      handleFileDownload(link, filename)
    }).catch(err => {
      console.error(err)
    })
}
        </script>
        <button onclick="handlePdfLink('http://192.168.0.194:1388/Report/ba8aecf1-bb22-4a3e-be2d-456c9c4645dc.pdf','测试文件.pdf')">下载</button>
    </body>
</html>