vue3 - - - - - post请求实现文件下载

vue3 - - - - - post请求实现文件下载

代码如下:

proxy
  .$https({
    
    
    url: `/api/export`,
    method: "post",
    data:{
    
    }, 
    responseType: "blob", // 设置响应值类型
  })
  .then(function (response) {
    
    
  	// 导出的是excel
    const blob = new Blob([response], {
    
    
      type: "application/vnd.ms-excel",
    });
    
    const downloadUrl = window.URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = downloadUrl;
    a.download = "文件名"; // 这里可以根据响应头或其他逻辑设置文件名
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(downloadUrl);
    document.body.removeChild(a);
  })
  .catch(function (error) {
    
    
    console.log(error);
  })

猜你喜欢

转载自blog.csdn.net/Dark_programmer/article/details/138852896