URLs對象 blob URL


把指向數據的URL保存到file或者blob對象里,好處就是不需要先文件讀取內容然后才能用。
 
function createObjectURL(blob){
if (window.URL){
return window.URL.createObjectURL(blob);
} else if (window.webkitURL){
return window.webkitURL.createObjectURL(blob);
} else {
return null;
}
}

返回一個URL字符串,指向內存地址,例如可以直接展示圖片文件出來:
var filesList = document.getElementById(“files-list”);
EventUtil.addHandler(filesList, “change”, function(event){
var info = “”,
output = document.getElementById(“output”),
progress = document.getElementById(“progress”),
files = EventUtil.getTarget(event).files,
reader = new FileReader(),
url = createObjectURL(files[0]);
if (url){
if (/image/.test(files[0].type)){
output.innerHTML = “<img src=\”” + url + “\”>”;
} else {
output.innerHTML = “Not an image.”;
}
} else {
output.innerHTML = “Your browser doesn’t support object URLs.”;
}
});
直接把URL對象輸出到img里,不需要把數據先讀取到JavaScript。 img標簽自己會從URL指向的內存地址拿圖片
 
當不用的時候,應該清除掉,釋放內存:
function revokeObjectURL(url){
if (window.URL){
window.URL.revokeObjectURL(url);
} else if (window.webkitURL){
window.webkitURL.revokeObjectURL(url);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM