### What problem does this PR solve? Feat: Support preview of HTML files #5096 ### Type of change - [x] New Feature (non-breaking change which adds functionality)tags/v0.17.0
| import Excel from './excel'; | import Excel from './excel'; | ||||
| import Pdf from './pdf'; | import Pdf from './pdf'; | ||||
| import { previewHtmlFile } from '@/utils/file-util'; | |||||
| import styles from './index.less'; | import styles from './index.less'; | ||||
| // TODO: The interface returns an incorrect content-type for the SVG. | // TODO: The interface returns an incorrect content-type for the SVG. | ||||
| const prefix = currentQueryParameters.get('prefix'); | const prefix = currentQueryParameters.get('prefix'); | ||||
| const api = `${api_host}/${prefix || 'file'}/get/${documentId}`; | const api = `${api_host}/${prefix || 'file'}/get/${documentId}`; | ||||
| if (ext === 'html' && documentId) { | |||||
| previewHtmlFile(documentId); | |||||
| return; | |||||
| } | |||||
| return ( | return ( | ||||
| <section className={styles.viewerWrapper}> | <section className={styles.viewerWrapper}> | ||||
| {Images.includes(ext!) && ( | {Images.includes(ext!) && ( |
| import api from '@/utils/api'; | import api from '@/utils/api'; | ||||
| import registerServer from '@/utils/register-server'; | import registerServer from '@/utils/register-server'; | ||||
| import request from '@/utils/request'; | import request from '@/utils/request'; | ||||
| import pureRequest from 'axios'; | |||||
| const { | const { | ||||
| listFile, | listFile, | ||||
| ); | ); | ||||
| export default fileManagerService; | export default fileManagerService; | ||||
| export const getDocumentFile = (documentId: string) => { | |||||
| return pureRequest(getFile + '/' + documentId, { | |||||
| responseType: 'blob', | |||||
| method: 'get', | |||||
| // headers: { | |||||
| // 'content-type': | |||||
| // 'text/plain;charset=UTF-8, application/vnd.openxmlformats-officeddocument.spreadsheetml.sheet;charset=UTF-8', | |||||
| // }, | |||||
| // parseResponse: false, | |||||
| // getResponse: true, | |||||
| }) | |||||
| .then((res) => { | |||||
| const x = res?.headers?.get('content-disposition'); | |||||
| const y = res?.headers?.get('Content-Type'); | |||||
| console.info(res); | |||||
| console.info(x); | |||||
| console.info('Content-Type', y); | |||||
| return res; | |||||
| }) | |||||
| .then((res) => { | |||||
| // const objectURL = URL.createObjectURL(res); | |||||
| // let btn = document.createElement('a'); | |||||
| // btn.download = '文件名.pdf'; | |||||
| // btn.href = objectURL; | |||||
| // btn.click(); | |||||
| // URL.revokeObjectURL(objectURL); | |||||
| // btn = null; | |||||
| return res; | |||||
| }) | |||||
| .catch((err) => { | |||||
| console.info(err); | |||||
| }); | |||||
| }; |
| return ''; | return ''; | ||||
| }; | }; | ||||
| async function fetchDocumentBlob(id: string, mimeType?: FileMimeType) { | |||||
| const response = await fileManagerService.getDocumentFile({}, id); | |||||
| const blob = new Blob([response.data], { | |||||
| type: mimeType || response.data.type, | |||||
| }); | |||||
| return blob; | |||||
| } | |||||
| export async function previewHtmlFile(id: string) { | |||||
| const blob = await fetchDocumentBlob(id, FileMimeType.Html); | |||||
| const url = URL.createObjectURL(blob); | |||||
| const link = document.createElement('a'); | |||||
| link.href = url; | |||||
| link.click(); | |||||
| URL.revokeObjectURL(url); | |||||
| } | |||||
| export const downloadFileFromBlob = (blob: Blob, name?: string) => { | export const downloadFileFromBlob = (blob: Blob, name?: string) => { | ||||
| const url = window.URL.createObjectURL(blob); | const url = window.URL.createObjectURL(blob); | ||||
| const a = document.createElement('a'); | const a = document.createElement('a'); | ||||
| id: string; | id: string; | ||||
| filename?: string; | filename?: string; | ||||
| }) => { | }) => { | ||||
| const response = await fileManagerService.getDocumentFile({}, id); | |||||
| const blob = new Blob([response.data], { type: response.data.type }); | |||||
| const blob = await fetchDocumentBlob(id); | |||||
| downloadFileFromBlob(blob, filename); | downloadFileFromBlob(blob, filename); | ||||
| }; | }; | ||||