Bootstrap

vue下载后端提供的文件/播放音频文件

下载文件

doc
接口js文件里加一行这个responseType: ‘arraybuffer’,
js文件

export function (data) {
  return request({
    url: "XXX",
    method: "post",
    data: data,
    baseURL: " ",
    responseType: 'arraybuffer',
  });
}

vue文件

const time={
    "datetime_start": start1.value,
    "datetime_end": end1.value,
  }
  ( time ).then((response) => {
    const data = response.data;
    let blob = new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8'});
    let objectUrl = window.URL.createObjectURL(blob) // 创建URL
    const a=document.createElement('a');
    a.style.display='none';
    a.href=objectUrl;
    a.download='报告.docx';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    objectUrl.revokeObjectURL(objectUrl); // 释放内存

});

播放音频

js文件

export function XXX(data) {
  return request({
    url: "XXX",
    method: "post",
    data: data,
    baseURL: " ",
    responseType: 'blob',
  });
}

vue文件

 const time = {
    audio_type: "2",
  };
  XXX(time).then((res) => {
   const blob = new Blob([res], { type: 'audio/wav' });
   const localUrl = (window.URL || webkitURL).createObjectURL(blob);
   const audio = document.createElement('audio');
   audio.style.display = 'none'; // 防止影响页面布局
   audio.controls = true;
   document.body.appendChild(audio);
   audio.src = localUrl;
   audio.playbackRate = 1.3; // 语速
   audio.play();
   
    // 语音播放完毕后,需要手动释放内存
    audio.onended = function () {
      document.body.removeChild(audio);
      URL.revokeObjectURL(localUrl);
    };

})
;