实现导入下载excel

1.下载

1.注意点:一般传输文件时请求需加上responseType:"blob"

export function downloadUrl(url, param) {
    return axios({
      url: url,
      method: 'Post',
      params:param,
      responseType:"blob"// 返回服务器返回的数据类型
    }).then((res) => {
      return Promise.resolve(res.data)
    }).catch((error) => {
      console.log(error)
    })
}
2.调用接口成功后注意后台返回的文件类型
new Blob的 type 参数,如果类型错误会提示与文件格式不一致

downloadUrl(url,param}).then(res => { // 接口请求
    let blob = new Blob([res], {type: 'application/vnd.ms-excel'})
    let elink = document.createElement('a'); 
    elink.download = name + '.xls' // 下载文件名称
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    document.body.removeChild(elink) //下载完成移除元素
    window.URL.revokeObjectURL(elink) //释放掉blob对象
})

2.导入

// myFile 为type=file的input框
let myFile = document.getElementById('leading-in').files[0]
   // 获取到文件不能直接传给后端,需要利用formData传给后端
    const formData = new FormData()
   // 'excelData' 需要和后端接收的参数保持一致,有多少参数就append多少
   formData.append('excelData',myFile) 
   formData.append('Code',code) 
   uploadExcel(url,formData).then((data) => {
       // 成功之后的处理
 })

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值