后端返回的数据,有tonken才能下载

<template>
  <div>
    <button @click="downloadFile">Download File</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  methods: {
    async downloadFile() {
      try {
        const token = 'your_token_here'; // 替换为实际的token
        const response = await axios.get('http://example.com/download', {
          responseType: 'blob',
          headers: {
            'Authorization': `Bearer ${token}`
          }
        });
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'file.pdf');
        document.body.appendChild(link);
        link.click();
      } catch (error) {
        console.error(error);
      }
    }
  }
}
</script>

猜你喜欢

转载自blog.csdn.net/weixin_60196946/article/details/131093494