window.URL对象的使用方式
window对象的URL对象是专门用来将blob或者file读取成一个url。
一、URL构造函数将普通url转换成URL对象
var url = new URL('https://my.oschina.net/u/4291402?name=test');
console.log('url' , url, url.searchParams.get("name")); //输出 test
二、URL.createObjectURL(object)
URL.createObjectURL(object)是URL对象的静态方法,其实就是返回了一个在内存中指向传入参数object的引用路径url字符串。生成的这个url字符串会在当前页面的document被销毁的时候失效。
三、URL.revokeObjectURL(objectURL)
用于销毁之前通过URL.createObjectURL(object)方法创建的url。
四、案例
文件下载
let content = new Blob();
const saveLink = document.createElement('a');
document.body.appendChild(saveLink);
const url = window.URL.createObjectURL(content); //content为一个文件或者blob
saveLink.href = url;
saveLink.download = 'filename';
saveLink.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(saveLink);
来源:oschina
链接:https://my.oschina.net/u/4291402/blog/3169334