Vue下载本地pdf文件

html:

<button @click="downloadPDf">downloadPDf</button>

js:

 downloadPDf() {
        axios.post('http://localhost:8080/static/test.pdf', {
          responseType: 'blob', //重要
        }).then(response => {
          const url = window.URL.createObjectURL(new Blob([response.data]));
          const link = document.createElement('a');
          let fname = 'report.pdf';
          link.href = url;
          link.setAttribute('download', fname);
          document.body.appendChild(link);
          link.click();
        })
      }
    }

记住本地文件一定要放在static文件夹下面。

Logo

前往低代码交流专区

更多推荐