【问题标题】:Converting data:image base64 to blob in this code在此代码中将 data:image base64 转换为 blob
【发布时间】:2020-01-03 14:26:19
【问题描述】:

我想将 URL 从 data:image base64 更改为 blob。这是生成 base64 url​​ 的原始代码:

<script>

    $(window).load(function(){

    function readURL() {
        var $input = $(this);
        var $newinput =  $(this).parent().parent().parent().find('.portimg ');
        if (this.files && this.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                reset($newinput.next('.delbtn'), true);
                $newinput.attr('src', e.target.result).show();
                $newinput.after('<div class="delbtn delete_upload"  title="Remove"><span class="bbb-icon bbb-i-remove2"></span></div>');


$("form").on('click', '.delbtn', function (e) {
    reset($(this));
    $("form").find('#rright-<?php echo $i;?>').hide();
  });
            }
            reader.readAsDataURL(this.files[0]);
        }
    }
    $(".file").change(readURL);


    function reset(elm, prserveFileName) {
        if (elm && elm.length > 0) {
            var $input = elm;
            $input.prev('.portimg').attr('src', '').hide();

            if (!prserveFileName) {
                $($input).parent().parent().parent().find('input.file ').val("");
                //input.fileUpload and input#uploadre both need to empty values for particular div
            }
            elm.remove();
        }
    }

    });

  </script>

我想要的是调用 Object.createObjectURL(this.files[0]) 来获取对象 URL,并将其用作 img 的 src; (只是不要打扰 FileReader)。

【问题讨论】:

  • 你已经在 this.files[0] 中拥有了 Blob,为什么还要花钱把它从 base 64 翻译回来?
  • @HereticMonkey 但是我如何得到它而不是 base64?
  • 你想从哪里获得 Blob?我的意思是您真正需要做的就是将this.files[0] 缓存在可以使用它的某个变量中。
  • @HereticMonkey 我应该更好地解释自己。现在链接看起来像这样:src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY0AAAGNCAIAAABixbqGAAAABGdBTUEAALGPC/xhBQAAAAlw......,它有一英里长。我希望它看起来像这样src="blob:https://mysite.c o m/98cd2703-9e3f-4115-8e20-abdc16fef4ad"
  • edit你的问题说清楚。那么你真正想要的只是调用Object.createObjectURL(this.files[0]) 来获取对象URL,并将其用作srcimg;只是不要打扰FileReader

标签: javascript jquery html


【解决方案1】:

这样的?

function readURL() {
  var file = this.files[0]
  var reader = new FileReader();
  var base64string = getBase64(file);
  reader.onload = function () {
    reset($newinput.next('.delbtn'), true);
    $newinput.attr('src', e.target.result).show();
    $newinput.after('<div class="delbtn delete_upload"  title="Remove"><span class="bbb-icon bbb-i-remove2"></span></div>');

    var blob = dataURItoBlob(base64string);
  };
  reader.onerror = function (error) {
    console.log('Error: ', error);
  };
}

【讨论】:

  • @ssten 我更新了我的原始帖子。这是我真正需要的。当我第一次发布我的问题时,我不确定如何表达。
【解决方案2】:

我不确定这是否可行,并且由于 Stack Snippets 的变幻莫测,无法在 Stack Overflow 上展示其可行性,但理论上,您应该能够使用 URL.createObjectURL 为您的图像,无需遍历整个 base 64 繁琐。

var $newinput =  $(this).parent().parent().parent().find('.portimg ');
if (this.files && this.files[0]) {
    $newinput.attr('src', URL.createObjectURL(this.files[0]));
    // if the above doesn't work, you could try to create a new Blob
    var fileBlob = new Blob(this.files[0], { type: "image/png" }) 
    // Substitute "image/png" with whatever image type it is
    $newinput.attr('src', URL.createObjectURL(fileBlob));

这应该为图像的源呈现适当的 URL。

请注意,最佳做法是在您使用完对象 URL 后将其撤消。我不确定在这种情况下是否有必要,因为大概您想在页面关闭之前显示图像。但是,如果用户可以上传新图片,请执行以下操作:

if ($newinput.attr('src').indexOf('blob') > -1) {
    URL.revokeObjectURL($newinput.attr('src'));
}

在设置新源之前添加它,您不必担心内存泄漏(无论如何都使用createObjectURL...)。

有关 Blob URL 的详细信息,请参阅 this answer 由现在匿名的用户发送至 What is a blob URL and why it is used?

【讨论】:

  • 对不起,它不工作...图像不显示,当我检查元素时那里什么也没有。
  • @Alex333 抱歉,这就是我多任务处理时发生的情况。 createObjectURLURL 对象上,而不是在 Object 上。
  • 嘿,已经完成了一半!当我检查元素时,新的 blob 链接就在那里,但是图像没有显示。代码中的某些内容阻止了图像显示……那可能是什么?这条线? $newinput.attr('src', e.target.result).show();?
  • 您可能需要指定图像的类型...这应该是您从this.files[0] 获得的文件 Blob 的一部分,但它可能不存在?我会尝试修改我的答案。
  • 它没有这样做。我想知道问题是否出在这一行reader.onload = function (e) { 我们没有读者了,因为这行已经消失了var reader = new FileReader();
猜你喜欢
  • 2013-09-10
  • 2015-03-14
  • 2018-10-07
  • 2016-03-20
  • 1970-01-01
  • 2021-07-27
  • 2021-06-20
  • 1970-01-01
  • 2014-10-28
相关资源
最近更新 更多