/**
* @description 文件预览
* @param { String } url 文件预览地址
* @return { String } 转换之后可预览的文件地址
* @example window.open(filePreviewUrl(url), '_blank')
*/
export function filePreviewUrl(url) {
let base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let result = ""
let padding = 0
let paddingChar = "="
for (let i = 0; i < url.length; i += 3) {
let b1 = url.charCodeAt(i)
let b2 = url.charCodeAt(i + 1)
let b3 = url.charCodeAt(i + 2)
let encoded1 = b1 >> 2
let encoded2 = ((b1 & 3) << 4) | (b2 >> 4)
let encoded3 = ((b2 & 15) << 2) | (b3 >> 6)
let encoded4 = b3 & 63
isNaN(b2) ? encoded3 = encoded4 = 64 : encoded4 = 64
result += base64Chars.charAt(encoded1) + base64Chars.charAt(encoded2) + base64Chars.charAt(encoded3) + base64Chars.charAt(encoded4)
}
return `${VUE_FILE_PREVIEW}?url=${encodeURIComponent(result)}`
}
法1:
window.open(filePreviewUrl(item.url), '_blank')
法2:window.open(`${VUE_FILE_PREVIEW}url=${encodeURIComponent(Base64.encode(url))}`)