记录-关于axios“GET”请求获取到pdf文件流下载的问题

文章讲述了作者在使用axios进行GET请求时,由于将请求参数和responseType分开写导致下载文件失败的经历,提醒其他开发者注意参数组织方式对axios响应的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不是使用axios的原因,也不是responseType未设置的原因,仅仅是因为我把请求参数和responseType分成俩对象导致被忽略了…具体get请求传参的可以去看官网,这里记录下因为传参导致我下载文件出的错误。
对比代码放在下面,希望和我碰到同样问题的同学能少踩坑

//正确写法
	axios.get(url, { params:{ xxx: 'xx' }, responseType: 'blob' }).then(res => {
        const link = document.createElement('a')
        let blob = new Blob([res.data])
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        const temp = decodeURI(
          res.headers.map['content-disposition'][0]
            .split(';')[1]
            .split('=')[1]
        )
        let iconv = require('iconv-lite')
        iconv.skipDecodeWarning = true
        let filename = iconv.decode(temp, 'UTF-8')
        link.setAttribute('download', filename)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })   
//错误写法
      axios.get(url, { xxx: 'xx' }, { responseType: 'blob' }).then(res => {
        const link = document.createElement('a')
        let blob = new Blob([res.data])
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        const temp = decodeURI(
          res.headers.map['content-disposition'][0]
            .split(';')[1]
            .split('=')[1]
        )
        let iconv = require('iconv-lite')
        iconv.skipDecodeWarning = true
        let filename = iconv.decode(temp, 'UTF-8')
        link.setAttribute('download', filename)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值