后端返回文件流,前端post请求方式接受并下载

版权声明:转载请注明出处 https://blog.csdn.net/weixin_43586120/article/details/89450746
fetch('<接口地址>', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: '<请求参数:json字符串>',
})
.then(res => res.blob())
.then(data => {
  let blobUrl = window.URL.createObjectURL(data);
  download(blobUrl);
});

function download(blobUrl) {
  const a = document.createElement('a');
  a.style.display = 'none';
  a.download = '导出的文件名';
  a.href = blobUrl;
  a.click();
  document.body.removeChild(a);
}

猜你喜欢

转载自blog.csdn.net/weixin_43586120/article/details/89450746