vue项目,调用接口下载文件の问题

如果是直接用a标签,href指向文件地址可以下载,但是如果调用接口去下
载就不成功了:

后端接口代码如下:
image

前端代码如下:
image
image

此时点击链接触发click事件返回的数据如下
image
image

请问是哪里出了问题吗?

已解决 悬赏分:0 - 解决时间 2021-11-27 00:40
反对 0举报 0 收藏 0

回答2

最佳
  • @

    downloadRFRepost 方法生成一个 a 标签, href 为下载地址, download 属性为文件名, click 它, 然后移除这个标签

    支持 0 反对 0 举报
    2021-11-26 07:49
  • @
    axios.get(url,{responseType: 'blob'})
    
    .then((response) => {
    
        let name =  this.name +  '.pdf';
    
        var blob = response.data;
    
        if (window.navigator.msSaveOrOpenBlob) {
    
        navigator.msSaveBlob(blob, name);
    
        } else {
    
        var a = document.createElement('a');
    
        a.download = name;
    
        a.style.display =  "none";
    
        a.href = URL.createObjectURL(blob);;
    
        document.body.appendChild(a); // 修复firefox中无法触发click
    
        this.isLoading =  false;
    
        a.click();
    
        document.body.removeChild(a)
    
        }
    
      })
    
      .catch(function(error) {
    
        console.log(error);
    
       });
    
    支持 0 反对 0 举报
    2021-11-26 09:13