将data:image base64在这段代码中转换为blob

huangapple go评论83阅读模式
英文:

Converting data:image base64 to blob in this code

问题

I'd like to change the URLs from data:image base64 to blob. This is the original code that produces the base64 urls:

<script>

    $(window).load(function(){

    function readURL() {
        var $input = $(this);
        var $newinput =  $(this).parent().parent().parent().find('.portimg ');
        if (this.files && this.files[0]) {
            var objectURL = URL.createObjectURL(this.files[0]);
            reset($newinput.next('.delbtn'), true);
            $newinput.attr('src', objectURL).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();
            });
        }
    }
    $(".file").change(readURL);


    function reset(elm, prserveFileName) {
        if (elm && elm.length > 0) {
            var $input = elm;
            $input.prev('.portimg').attr('src', '');
            
            if (!prserveFileName) {
                $($input).parent().parent().parent().find('input.file ').val("");
                //input.fileUpload and input#uploadre both need to empty values for a particular div
            }
            elm.remove();
        }
    }

    });

</script>

What I want is to call URL.createObjectURL(this.files[0]) to get the object URL and use that as the src of your img; (just don't even bother with the FileReader).

英文:

I'd like to change the URLs from data:image base64 to blob. This is the original code that produces the base64 urls:

&lt;script&gt;

    $(window).load(function(){
      
    function readURL() {
        var $input = $(this);
        var $newinput =  $(this).parent().parent().parent().find(&#39;.portimg &#39;);
        if (this.files &amp;&amp; this.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                reset($newinput.next(&#39;.delbtn&#39;), true);
                $newinput.attr(&#39;src&#39;, e.target.result).show();
                $newinput.after(&#39;&lt;div class=&quot;delbtn delete_upload&quot;  title=&quot;Remove&quot;&gt;&lt;span class=&quot;bbb-icon bbb-i-remove2&quot;&gt;&lt;/span&gt;&lt;/div&gt;&#39;);
            
                
$(&quot;form&quot;).on(&#39;click&#39;, &#39;.delbtn&#39;, function (e) {
    reset($(this));
    $(&quot;form&quot;).find(&#39;#rright-&lt;?php echo $i;?&gt;&#39;).hide();
  });
            }
            reader.readAsDataURL(this.files[0]);
        }
    }
    $(&quot;.file&quot;).change(readURL);


    function reset(elm, prserveFileName) {
        if (elm &amp;&amp; elm.length &gt; 0) {
            var $input = elm;
            $input.prev(&#39;.portimg&#39;).attr(&#39;src&#39;, &#39;&#39;).hide();
            
            if (!prserveFileName) {
                $($input).parent().parent().parent().find(&#39;input.file &#39;).val(&quot;&quot;);
                //input.fileUpload and input#uploadre both need to empty values for particular div
            }
            elm.remove();
        }
    }

    });

  &lt;/script&gt;

What I want is to call Object.createObjectURL(this.files[0]) to get the object URL, and use that as the src of your img; (just don't even bother with the FileReader).

答案1

得分: 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);
  };
}
英文:

Something like this?

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

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

答案2

得分: -1

I can provide a Chinese translation for the code-related content you provided:

"我不能确定这是否会起作用,由于 Stack Snippets 的不确定性,无法在 Stack Overflow 上演示其可行性,但理论上,您应该能够使用 URL.createObjectURL 来创建适用于您的图像的正确 URL,而不必经过整个 base 64 的繁琐过程。

var $newinput = $(this).parent().parent().parent().find('.portimg ');
if (this.files && this.files[0]) {
    $newinput.attr('src', URL.createObjectURL(this.files[0]));
    // 如果上面的方法不起作用,您可以尝试创建一个新的 Blob
    var fileBlob = new Blob(this.files[0], { type: "image/png" })
    // 用实际的图像类型替代 "image/png"
    $newinput.attr('src', URL.createObjectURL(fileBlob));
}

这应该生成图像来源的正确 URL。

请注意,最佳实践是在使用完对象 URL 后吊销它。我不确定在这种情况下是否有必要,因为您可能希望在关闭页面之前显示图像。但是,如果用户可以上传新图像,请添加以下代码:

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

在设置新来源之前添加这些代码,您就不必担心内存泄漏(至少在使用 createObjectURL 方面不必担心...)。

有关 Blob URL 的更多信息,请参阅 此答案(由一位匿名用户提供)什么是 Blob URL,为什么使用它?。"

英文:

I'm not sure if this will work and due to the vagaries of Stack Snippets, can't demonstrate its viability here on Stack Overflow, but theoretically, you should be able to use URL.createObjectURL to create the appropriate URL for your image, without going through the whole base 64 rigmarole.

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

That should render the appropriate URL for the image's source.

Note that it is best practice to revoke the object URL when you are done with it. I'm not sure that's necessary in this case, since presumably you want to show the image until the page is closed. However, if the user can upload a new image, do something like:

if ($newinput.attr(&#39;src&#39;).indexOf(&#39;blob&#39;) &gt; -1) {
    URL.revokeObjectURL($newinput.attr(&#39;src&#39;));
}

Add that before setting the new source and you shouldn't need to worry about memory leaks (from this use of createObjectURL anyway...).

For more information on Blob URLs, see this answer by a now-anonymous user to What is a blob URL and why it is used?

huangapple
  • 本文由 发表于 2020年1月3日 22:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定