vue3请求接口返回文件流并下载

接口地址:注意一定要加上:responseType: 'blob'

export const exportData=(data:any)=>{
  return request.post({
    url:'/Order/ExportByExt',
    data,
    responseType: 'blob',
  })
}

导出按钮函数:

const exportX=async ()=>{
  await getData()
  let filter=JSON.parse(JSON.stringify(data.filter))
  filter.pageModel.pageCurrent=1
  data.loading=true
  const res=await unifyPayApi.exportData(filter).catch(e=>{
    ElMessage.error(e)
    data.loading=false
  })
  console.log(res)
    let url = window.URL.createObjectURL(new Blob([res.data],{ type: 'application/vnd.ms-excel' }));
    let a = document.createElement("a");
    a.style.display = "none";
    a.href = url;
    a.setAttribute("download", '订单管理-独立科室'+moment().format('YYYY-MM-DD-HH-mm-ss')+".xlsx");
    document.body.appendChild(a);
    a.click(); //执行下载
      document.body.removeChild(a); //释放标签
      window.URL.revokeObjectURL(a.href); //释放url
    data.loading=false

}

打印出来的文件流信息:

{
    "data": {},
    "status": 200,
    "statusText": "OK",
    "headers": {
        "content-length": "2794",
        "content-type": "application/octet-stream"
    },
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "adapter": [
            "xhr",
            "http"
        ],
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 60000,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "env": {},
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhbmdwdSIsImlhdCI6IjE2ODMxOTMxNDMiLCJuYmYiOiIxNjgzMTkzMTQzIiwiZXhwIjoiMTY4ODM3NzE0MyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZXhwaXJhdGlvbiI6IjIwMjMvNy8zIDE3OjM5OjAzIiwiaXNzIjoiU3lzdGVtIiwiYXVkIjoiVXNlciIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IjEiLCJVc2VySWQiOiJhbmdwdSIsIk9wZW5JZCI6IiIsIlNvdXJjZSI6IiJ9.XSLwwb8RtTdHG_6JRHPaHVOmvDEPRY0DZvOrRtEHkQo"
        },
        "baseURL": "http://192.168.1.188:6001",
        "url": "/Order/ExportByExt",
        "method": "post",
        "data": "{\"hosp\":[],\"dateBegin\":\"2022-05-05\",\"dateEnd\":\"2023-05-05\",\"UserID\":\"\",\"hospitalID\":\"\",\"useExt2\":\"true\",\"ext3\":\"\",\"no\":\"\",\"name\":\"\",\"cardNo\":\"\",\"payType\":[],\"payScene\":\"\",\"bizType\":\"\",\"pageModel\":{\"pageCurrent\":1,\"pageSize\":10}}",
        "responseType": "blob"
    },
    "request": {}
}

image.png

复制内容


评论


乖,登录后才可以留言! 登录

Copyright © 2020-2023 春藤技术,春藤建站 All Rights Reserved
备案号:豫ICP备20020705号 公网安备 51LA统计