下载 mp3、png、vtt、srt 等文件到本地,亲测有效
export const downloadFromUrl = (url: string, name?: string) => {
try {
const ajax = new XMLHttpRequest();
ajax.open('GET', url.replace('http://', 'https://'), true);
ajax.responseType = 'blob';
ajax.onload = function() {
const content = ajax.response;
let src = (window.URL || window.webkitURL).createObjectURL(new Blob([content]));
const a = document.createElement('a');
a.href = src;
a.download = name || '';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
ajax.send();
} catch(e) {
console.log('download: ', e);
}
}