使用fetch请求,如何获取响应头Content-Disposition中的文件名

fetch(url, {
        method: 'get',
        responseType: 'blob',
        headers: {
          Authorization: 'Bearer ' + store.getters.token,
        },
      })
        .then(res => {
          // 获取响应头
          const fileNameEncode = res.headers
            .get('Content.Disposition')
            .split('filename=')[1]
          fileName = decodeURIComponent(fileNameEncode)
          return res.blob()
        })
        .then(res => {
          const url = URL.createObjectURL(res)
          const aLink = document.createElement('a')
          aLink.href = url
          aLink.setAttribute('download', fileName)
          document.body.appendChild(aLink)
          aLink.click()
          document.body.removeChild(aLink)
        })

你可能感兴趣的:(前端,javascript)