a标签下载和文件流下载

a标签直接下载

export const downloadFile = (obj, fileName) => {
  const url = window.URL.createObjectURL(new Blob([obj]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

a标签下载跨域解决办法

 axios({
   responseType: "blob",
   url: fileUrl, // 下载跨域的地址
 }).then((res) => {
   const url = window.URL.createObjectURL(res.data);
   const link = document.createElement("a");
   link .href = url;
   link .download = ""; // 文件名
   link .click();
   window.URL.revokeObjectURL(url)
 });

// blob 响应类型的接口 转换为 json类型

onUploadFileSuccess(res) {
      console.log('onUploadFileSuccess', res)
      const reader = new FileReader()
      reader.onload = () => {
        console.log(reader)
        const data = JSON.parse(reader.result)
        if (data.code === 200) {
          this.$message({ message: '导入成功', type: 'success' })
        } else {
          this.$message({ message: data.message, type: 'error' })
        }
        this.getList()
      }
      reader.readAsText(res.data)
    },

你可能感兴趣的:(js基础,javascript,前端)