js实现文件下载(带参)

71 阅读1分钟
fetch(url, 
    {
      method: 'POST', 
      headers: {
      'Content-Type': 'application/json'
      },
      body: params && JSON.stringify(params)
    }
    )
      .then(response => response)
      .then(res => {
        debugger
          let blobUrl = window.URL.createObjectURL(res.blob())
          let a = document.createElement('a')
          a.href = blobUrl;
          const now = moment().format('YYYYMMDDHHmmss');
          a.download = `${now}.xls`;
          a.click()
          window.URL.revokeObjectURL(url)
  })