js 前端下载后端返回的文件流

/**
 *
 *
 * @export
 * @param {*} fileRes文件流
 * @param {{ name: any; type: any }} fileObj文件名称和类型
 */
export function downFileByStream(
  fileRes: any,
  fileObj: { name: any; type: any }
) {
  const url = window.URL.createObjectURL(new Blob([fileRes]));
  const aLink = document.createElement("a");
  aLink.style.display = "none";
  aLink.href = url;
  //可自定义名称
  aLink.setAttribute("download", fileObj.name+'.'+fileObj.type);
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink);
  window.URL.revokeObjectURL(url);
}


版权声明:本文为animatecat原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。