下载流文件

42 阅读1分钟
    if (response.data.type === 'application/json') {
      let self = this
      var reader = new FileReader()  // 读取文本       
      reader.onload = function(event) {
        // 文件里的文本会在这里被打印出来
        let result = JSON.parse(reader.result) // event.target.result
        if (result.code) {
          self.$message.error(result.msg)
        }
      }
      reader.readAsText(response.data)
      return     
    }
    const blob = new Blob([response.data], { type: 'application/octet-stream' }) // 下载文件的文件类型
    const filename = decodeURIComponent(response.headers['filename']) // 下载文件的文件名
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = filename
    document.body.appendChild(link)
    link.click()
    window.setTimeout(function() {
      URL.revokeObjectURL(blob)
      document.body.removeChild(link)
    }, 0)