我的网页生成如下网址:blob:http%3A // localhost%3A8383 / 568233a1-8b13-48b3-84d5-cca045ae384f如何转换它到正常地址?
My page generates a URL like this: "blob:http%3A//localhost%3A8383/568233a1-8b13-48b3-84d5-cca045ae384f" How can I convert it to a normal address?
我用它作为< img> 的 src 属性。
推荐答案从JavaScript Blob 无法转换为普通网址。
A URL that was created from a JavaScript Blob can not be converted to a "normal" URL.
A blob: URL不是指服务器上存在的数据,它指的是当前页面中浏览器当前在内存中的数据。它不会在其他页面上提供,它将无法在其他浏览器中使用,也不会从其他计算机上获得。
A blob: URL does not refer to data the exists on the server, it refers to data that your browser currently has in memory, for the current page. It will not be available on other pages, it will not be available in other browsers, and it will not be available from other computers.
因此它没有意义,通常,将 Blob URL转换为普通URL。如果你想要一个普通的URL,你必须将数据从浏览器发送到服务器,让服务器像普通文件一样使用它。
Therefore it does not make sense, in general, to convert a Blob URL to a "normal" URL. If you wanted an ordinary URL, you would have to send the data from the browser to a server and have the server make it available like an ordinary file.
这是可能的将 blob:网址转换为数据:网址,至少在Chrome中。您可以使用AJAX请求从 blob: URL中获取数据(即使它实际上只是将其从浏览器的内存中拉出来,而不是发出HTTP请求) 。
It is possible convert a blob: URL into a data: URL, at least in Chrome. You can use an AJAX request to "fetch" the data from the blob: URL (even though it's really just pulling it out of your browser's memory, not making an HTTP request).
以下是一个例子:
var blob = new Blob(["Hello, world!"], { type: 'text/plain' }); var blobUrl = URL.createObjectURL(blob); var xhr = new XMLHttpRequest; xhr.responseType = 'blob'; xhr.onload = function() { var recoveredBlob = xhr.response; var reader = new FileReader; reader.onload = function() { var blobAsDataUrl = reader.result; window.location = blobAsDataUrl; }; reader.readAsDataURL(recoveredBlob); }; xhr.open('GET', blobUrl); xhr.send();
数据: URL可能不是正常的意思,可能会有问题。但是,它们的工作方式与普通URL一样,因为它们可以共享;它们并不特定于当前的浏览器或会话。
data: URLs are probably not what you mean by "normal" and can be problematically large. However they do work like normal URLs in that they can be shared; they're not specific to the current browser or session.
更多推荐
将blob URL转换为普通URL
发布评论