简体   繁体   English

Blob图片网址(javascript)

[英]blob image url (javascript)

I try to use blob to show image in website but how I can get URL outside onload to return it in function? 我尝试使用Blob在网站中显示图片,但是如何在onload之外获取URL以将其返回给函数?

var xhr = new XMLHttpRequest();
xhr.open("GET", "//fiddle.jshell.net/img/logo.png", true);
xhr.responseType = "arraybuffer";

xhr.onload = function(e) {
    var arrayBufferView = new Uint8Array(this.response);
    var blob = new Blob([arrayBufferView]);
    var urlCreator = window.URL || window.webkitURL;
    var imageUrl = urlCreator.createObjectURL(blob);
    //window.URL.revokeObjectURL(imageUrl);
};

xhr.send();

if you use imageUrl=urlCreator.createObjectURL(blob); 如果使用imageUrl=urlCreator.createObjectURL(blob); instead var imageUrl=urlCreator.createObjectURL(blob); 而是var imageUrl=urlCreator.createObjectURL(blob); you will put your variable in global scope and be able to access outside the function. 您将把变量放在全局范围内,并能够在函数外部访问。

function yo(lo) {
window.onload=function(e){
lo("give me shiny gold");
}}


yo(function(work) {
alert(work);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM