简体   繁体   中英

Converting image blob url to base64

I have this code from a library and I wanted to convert the image part to base64.

But I keep on having error on converting blob to base64.

It is a local html file hosted on the device.

      <div class="widget file-picker"><input class="ignore fake-file-input"
      placeholder="Click here to upload file. (< 5MB)" readonly=""><button class="btn-icon-only btn-reset"
      aria-label="reset" type="button"><i class="icon icon-refresh"> </i></button><a
      class="btn-icon-only btn-download" aria-label="download"
      download="image-1562651713366.jpg389038873-13_55_28.jpg"
      href="blob:file:///3efb6b21-718c-48d2-8fa1-f59520228804"><i class="icon icon-download"> </i></a>
    <div class="file-feedback "></div>
    <div class="file-preview"><img src="blob:file:///3efb6b21-718c-48d2-8fa1-f59520228804"></div>
  </div>

Here is may sample code on converting img to base64. It seems its not working on blob urls.

var someimage = $(".file-preview").find("img");
var canvas = document.createElement("canvas");
canvas.width = someimage.width;
canvas.height = someimage.height;
canvas.getContext("2d").drawImage(someimage, 0, 0);
dataURI = canvas.toDataURL();

Updated to incorporate the comment from Rory McCrossan

Wait for the image to load and be aware that someimage is a jQuery object

var someimage = $(".file-preview").find("img");
someimg.on("load", function () { 
    var canvas = document.createElement("canvas");
    canvas.width = someimage.width;
    canvas.height = someimage.height;
    canvas.getContext("2d").drawImage(someimage[0], 0, 0);
    dataURI = canvas.toDataURL();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM