admin管理员组

文章数量:1659044

谷歌内核正常,IE下载无后缀名,打不开

  • 本次项目需要下载的文件就两种格式xlsx和zip
  • 所以走ie时手动添加后缀名就可以搞定了
export const download = async({ url, params, method = 'post', type = 'xlsx' }) => {
  const isget = method.toLowerCase() === 'get';
  const obj = isget ? { params } : { data: params };
  const baseURL = process.env.VUE_APP_BASE_API + url
  try {
    const { data } = await axios({ baseURL, method, ...obj })
    if (data.code) {
      window.alert(data.message || '网络繁忙,请稍后重试');
    } else {
      axios({ baseURL, method, ...obj, responseType: 'arraybuffer' })
        .then(res => {
          res._params = params
          _download(res, type);
        }).catch(err => {
          window.alert(err.message || '网络繁忙,请稍后重试');
        })
    }
  } catch (error) {
    console.log(error);
  }
}
const _download = (res, _type) => {
  const type = _type === 'xlsx' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'application/zip';
  const fuffix = _type === 'xlsx' ? '.xlsx' : '.zip'
  const content = res.data
  const blob = new Blob([content], { type });
  const fileName = res._params.fileName || '';
  if ('download' in document.createElement('a')) { // 非IE下载
    const elink = document.createElement('a')
    elink.download = window.decodeURIComponent(fileName);
    elink.style.display = 'none'
    elink.href = window.URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    window.URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else { // IE10+下载
    navigator.msSaveBlob(blob, fileName + fuffix )
  }
}

本文标签: 文件后缀名