将blob URL转换为普通URL

编程入门 行业动态 更新时间:2024-10-23 04:50:04
本文介绍了将blob URL转换为普通URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的网页生成如下网址: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

本文发布于:2023-11-01 12:32:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   blob   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!