从 URL 创建对象

Create object from URL

本文关键字:创建对象 URL      更新时间:2023-09-26

有没有办法像这样从URL创建对象/blob对象:

blob:http://127.0.0.1:8888/4bd9114b-1adb-40ce-b55b-a38f803b849a

像这样:blob:111d6876-dc9c-4ec5-84a1-1004cae101b4

这是我到目前为止尝试过的代码:

    var xhr = new XMLHttpRequest();
    xhr.open('GET', source, true);
    xhr.responseType = 'blob';
    xhr.onload = function(e) {
      alert('Response status - ' + this.status);    
      if (this.status == 200) {
        var myBlob = this.response;
        alert("Converted to Blob");
      }
    };
    xhr.send();

但回应总是this.status 0

更新:

斑点来自剪贴板

这是一个开始,它应该回答您指定的第一个 url。

请参阅 https://developer.mozilla.org/en-US/docs/Web/API/Blob和https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

var blobPart=["http%3A//127.0.0.1%3A8888/4bd9114b-1adb-40ce-b55b-a38f803b849a"];
var blob = new Blob(blobPart, {type: "application/octet-binary"}); // pass a useful mime type here
console.log("blob ~ ", blob);
var urlObj = URL.createObjectURL(blob);
console.log("url ~", urlObj);
 //using FileReader to read Blob
var reader = new FileReader();
reader.addEventListener("loadend", function() {
   console.log("reader result ~ ",reader.result); 
});
reader.readAsDataURL(blob);

请参阅控制台:http://jsfiddle.net/Seandeburca/P9HRa/