通过文件url地址,实现文件下载功能
export const downloadTemple = (url: string, filename: string) => {
if (!!(window as any).ActiveXObject || 'ActiveXObject' in window) {
(window.navigator as any).msSaveOrOpenBlob(url, filename)
} else {
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
}
通过文件地址下载
export function downloadTemple1(url: string) {
const ele = document.createElement('a');
ele.setAttribute('href', url);
ele.target = '_self';
document.body.appendChild(ele);
ele.click();
document.body.removeChild(ele);
}
文件流方式下载
export function downLoadFile(data: any, filename: string) {
let blob = new Blob([data], { type: `application/text;charset=utf-8` });
let downloadElement = document.createElement('a');
let href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = filename;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
}
文件预览
export const handleReviewCreateA = (item: string) => {
let url = item;
let doc: Document = document,
a: HTMLAnchorElement = doc.createElement('a')
if (doc.body.querySelector('#a-preview-pdf-temp')) {
doc.body.removeChild(doc.body.querySelector('#a-preview-pdf-temp') as Node)
}
a.id = 'a-preview-pdf-temp'
a.setAttribute('target', '_blank')
a.href = url
doc.body.appendChild(a)
a.click()
doc.body.removeChild(a)
}