Newer
Older
xc-business-system / src / commonMethods / useOpenPdfinBrowser.ts
import Axios from 'axios'
import { getPhotoUrl } from '@/api/file'
/**
 * 在浏览器的安全与隐私已经设置了在浏览器中直接打开文档的前提下
 * 存在有的pdf文件直接能在浏览器中打开,有的文件由于浏览器的安全属性,会触发浏览器的下载
 * 此函数解决部分文件不能直接在浏览器中打开的问题
 */
export default function useOpenPdfinBrowser(filename: string) {
  getPhotoUrl(filename).then((res: any) => {
    Axios.get(res.data, { responseType: 'blob' }).then((data: any) => {
      const responseData = data.data
      const blob = new Blob([responseData], {
        type: 'application/pdf;charset=utf-8',
      })
      const href = URL.createObjectURL(blob)
      window.open(href, 'newWindow')
    })
  })
}