将blob URL转换为普通URL [英] Convert blob URL to normal URL

查看:518
本文介绍了将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 <$ c $创建的网址c> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆