JavaScript使用a标签下载文件
function(file_type){ let url = '文件的url地址' let file_suffix = { // 后缀 img: '.jpg', video: '.mp4', } let file_name = 'file' + file_suffix[file_type] // 文件名 let create_a_dom = document.createElement('a') document.body.append(create_a_dom) create_a_dom.href = url create_a_dom.download = file_name create_a_dom.rel = 'noopener noreferrer' create_a_dom.click() document.body.removeChild(create_a_dom) }
svg字符串(res就是svg的字符串)转为URL后下载:
let blob = new Blob([res], { type: 'image/svg+xml', }) let url = URL.createObjectURL(blob) let create_a_dom = document.createElement('a') document.body.append(create_a_dom) create_a_dom.href = url create_a_dom.download = this.download_file.name create_a_dom.rel = 'noopener noreferrer' create_a_dom.click() document.body.removeChild(create_a_dom)