前端下载文件方式(Blob)
以下以下载图标svg文件为例,实现点击按钮下载文件,其中icon结构如下:
const DownloadSvg = (props) => {function download(downfile) {const tmpLink = document.createElement("a");const objectUrl = URL.createObjectURL(downfile);tmpLink.href = objectUrl;tmpLink.download = downfile.name;document.body.appendChild(tmpLink);tmpLink.click();document.body.removeChild(tmpLink);URL.revokeObjectURL(objectUrl);}return <div><Button className='center-field' onClick={() => {const { icon } = propsconsole.log(icon);// window.open打开新页签进行文件预览 可手动下载// window.open(icon.url)const file = new File([icon.icon_svg], `${icon.font_class}.svg`, {type: "image/svg",});download(file)}} >点击下载图标svg</Button></div>
}