Bootstrap

vue中后端返回文件流( type: “application/octet-stream“ )的形式,前端进行处理和文件下载,以及自定生成对应的文件类型

// 下载文件
export function downFile(id,filename) {
  return request({
    url: `xxx/${id}`,
    method: 'get',
    responseType: 'blob',
  }).then((res) => {
    var blob = new Blob([res.data])
    console.log(blob)
    var downloadUrl = window.URL.createObjectURL(blob)
    var anchor = document.createElement('a')
    //anchor.style.display = "none";
    anchor.href = downloadUrl
    // 这里的filename 带有后缀,能决定文件的类型
    anchor.setAttribute("download", filename);
    document.body.appendChild(anchor)
    anchor.click()
    document.body.removeChild(anchor)
    //window.URL.revokeObjectURL(data)
  }
  )
}
;