下载文件流

2023-09-20  本文已影响0人  而生lhw
// 通用文件下载方法 post请求
export function download(url, params, filename) {
  return service.post(url, params, {
    transformRequest: [(params) => {
      return tansParams(params)
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    responseType: 'blob'
  }).then((data) => {
    const content = data
    const blob = new Blob([content])
    if ('download' in document.createElement('a')) {
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href)
      document.body.removeChild(elink)
    } else {
      navigator.msSaveBlob(blob, filename)
    }
  }).catch((r) => {
    console.error(r)
  })
}
// pdf下载方法   post请求
export function downloadPDF(url, params, filename) {
  return service.post(url, params, {
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    responseType: 'blob'
  }).then((res) => {
    const pdfFile = window.URL.createObjectURL(
      new Blob([res], { type: "application/pdf" })
    );
    // 跳转页面预览
    window.open(pdfFile);
    URL.revokeObjectURL(pdfFile); //释放URL对象

  }).catch((r) => {
    console.error(r)
  })
}
// 通用下载方法  post请求,json数据
export function downloadJSON(url, params, filename) {
  return service.post(url, params, {
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    responseType: 'blob'
  }).then((data) => {
    const content = data
    const blob = new Blob([content])
    if ('download' in document.createElement('a')) {
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href)
      document.body.removeChild(elink)
    } else {
      navigator.msSaveBlob(blob, filename)
    }
  }).catch((r) => {
    console.error(r)
  })
}
// 通用下载方法  get请求
export function downloads(url, params, filename) {
  let urls = url
  return service.get(urls, {
    params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    responseType: 'blob'
  }).then((data) => {
    const content = data
    const blob = new Blob([content])
    if ('download' in document.createElement('a')) {
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href)
      document.body.removeChild(elink)
    } else {
      navigator.msSaveBlob(blob, filename)
    }
  }).catch((r) => {
    console.error(r)
  })

}

// 通用下载方法  post请求  formdata数据
export function downloadFormData(url, params, filename) {
  // params
  let formData = new FormData()
  formData.append('fileName',params.filename)
  formData.append('filePath',params.filePath)
  return service.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    responseType: 'blob'
  }).then((data) => {
    const content = data
    const blob = new Blob([content])
    if ('download' in document.createElement('a')) {
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href)
      document.body.removeChild(elink)
    } else {
      navigator.msSaveBlob(blob, filename)
    }
  }).catch((r) => {
    console.error(r)
  })
}
上一篇下一篇

猜你喜欢

热点阅读