vue ts 文件导入导出

12 篇文章 0 订阅
6 篇文章 0 订阅
  1. Axios配置

/**********************下面代码是在相应拦截器下做的处理**************************/


// dataType 是自己封装的判断类型的工具函数,不重要
if (dataType(response.data) === 'Blob') {
  // 构造一个 Blob 对象 并设置文件类型
  const blob = new Blob([response.data], {
    type: 'application/vnd.ms-excel',
  })
  // 创建新的URL表示指定的blob对象
  const href = URL.createObjectURL(blob)
  const a = document.createElement('a')
  // 不隐藏可能会闪屏
  a.style.display = 'none'
  // 指定下载链接
  a.href = href
  // 指定下载文件名,这里也可以根据后端返回的文件名生成文件
  a.download = '订单'
  a.click()
  // 释放URL对象
  URL.revokeObjectURL(a.href)
}
  1. 导出处理的代码

// 导出主要就是给一个 返回类型配置 blob,下面是经过封装的get方法
export const exportExcelListApi = (params: { categoryId: number }) => {
  return get<any>('/erp/sku/exportSkuByType', params, {
    responseType: 'blob',
  })
}
// 使用axios实例请求则是这样
export const orderListExportApi = (data: exportParamsType): Promise<any> => {
  return axios({
    url: '/order/agentExport',
    method: 'post',
    data,
    responseType: 'blob',
  })
}
  1. 导入的处理代码

/**
 * @function: 导入sku列表数据
 * @params: any
 * @return:
 */
export const importExcelListApi = (data: {
  file: Blob
  categoryId: number
}) => {
  return post<any>('/erp/sku/importSkuByType', data)
}

// 在调用的地方需像下面这样处理
const { file } = fileData
const form = new FormData()
// 转成文件格式的数据,所有参数都放进form里面,让form对象作为参数
form.append('file', file)
form.append('categoryId', String(props.categoryId))
// 直接调用接口即可,切记只要能由form对象作为唯一参数(但是form对象里面可有多个参数)传进去
const res = await importExcelListApi(form as any)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZL随心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值