vue实现pdf+doc预览

131 阅读1分钟
// 安装 npm i docx-preview --save
import { renderAsync } from "docx-preview"
// doc 处理
function getDocxContent(url: string) {
	try {
		previewRef.value.open()
		fetch(url)
			.then(response => response.blob())
			.then(blob => {
				renderAsync(blob, previewContentRef.value)
			})
	} catch (err) {
		previewRef.value.close()
		console.error(err)
	}
}
// 下载
function createElementA(url: string) {
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  // link.target = '_blank'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
fun(name: string, id: string){
	const url = `${ import.meta.env.VITE_API_PREFIX } /attachment/download / ${ id }?Authorization = ${ localStorage.token } `
	if (name.includes("pdf")) {
			fetch(url)
				.then(response => response.blob())
				.then(blob => {
					const url = URL.createObjectURL(blob)
					window.open(url, "_blank")
				})
		} else if (name.includes("doc")) {
			getDocxContent(url)
		}
}