js 下载流调用浏览器预览 的方法

发布时间 2023-08-26 14:43:55作者: 7c89
//文件预览
var $viewblob=function (url, data) {
  const config = { responseType: 'blob', timeout: 9999999 };
  $http(url,data,config).then(res => {
    const fileRes =res;
    let type = { type: 'application/octet-stream' };
    const types = {
        pdf: { type: 'application/pdf' },
        png: { type: 'image/png' },
        jpg: { type: 'image/jpeg' },
    }
    if (window['fileDownName']) {
        const suffix = window['fileDownName'].substring(window['fileDownName'].lastIndexOf('.') + 1).toLocaleLowerCase();
        if (types[suffix]) {
            type = types[suffix];
        }
    }

    let viewurl= window.URL && window.URL.createObjectURL(new Blob([fileRes.blob], type));
    if(viewurl){
      window.open(viewurl)
    }
  })
}