如何将背景图像设置为 blob url

时间:2021-03-31 14:20:53

标签: html background blob

我正在尝试将我的背景图像设置为 blob url。我有代码

var t = localStorage.getItem('customwallpaper');
var test = t
const blob = new Blob([test], {type: 'text/plain'});
var url = window.URL.createObjectURL(blob);
document.body.style.backgroundImage = 'url( ' + url + ' )'

当我刷新页面时,我可以进入控制台,没有错误,并且在元素选项卡中,背景图像设置为 blob url。我做错了什么?

编辑:本地存储变量是一个 base64 编码的图像。

1 个答案:

答案 0 :(得分:0)

您的回答似乎与这个问题非常相似:Set background-image to a blob: URI

The answer

JosephDFGX 可能对您有用:

<块引用>
<script>
var tmp_path = URL.createObjectURL('path/to/image.png');
document.getElementbyId('divId').style.background = tmp_path;
</script>
相关问题