简体   繁体   English

使用Blob作为src映像

[英]Use blob as src image

I'm storing images as blob in my MySQL BDD. 我将图像作为Blob存储在我的MySQL BDD中。
I call my node API that return me my BDD lines with JQuery Ajax : 我调用我的节点API,使用JQuery Ajax返回我的BDD行:

$.ajax({
    url: "http://mydinners.fr:5280/partner",
    type: "GET",
    datatype: "json",
    contentType: "application/json; charset=utf-8",
    success: function(text){
        for(var i=0; i<text["message"].length; i++){
            var objurl = window.URL.createObjectURL(new Blob(text["message"][i]["partner_image"]));

            // text["message"][i]["image"] is an array of point like : [192,257,54,269,85,458,...]

            var module = "";
            module += "<img class='activator' src='"+objurl+"'>";

            $(".container .row").append(module);
        }
    }
});

The objurl value is something like this : "blob:http%3A//dev.mydinners.fr/e73c5c6f-562e-4b66-9d4e-7a4c8567532e" objurl值类似于:“ blob:http%3A // dev.mydinners.fr / e73c5c6f-562e-4b66-9d4e-7a4c8567532e”

But the image is like this : http://i.stack.imgur.com/Vhma7.png 但是图像是这样的: http : //i.stack.imgur.com/Vhma7.png

Do you know how to translate my blob image from mysql to img ? 您知道如何将我的Blob图片从mysql转换为img吗? thx. 谢谢。

You have to convert the array to a binary data (using Uint8Array) before creating the blob 您必须在创建Blob之前将数组转换为二进制数据(使用Uint8Array)

var binary = new Uint8Array(json.message[0].partner_image)

This is somewhat irrelevant, but it's a working example when you use Uint8Array, I just choose my own way of fetching/printing the data 这有点无关紧要,但是当您使用Uint8Array时,这是一个有效的示例,我只是选择自己的方式来获取/打印数据

 fetch("http://mydinners.fr:5280/partner") .then(res => res.json()) .then(json => { let binary = new Uint8Array(json.message[0].partner_image) let blob = new Blob([binary]) let img = new Image() img.src = URL.createObjectURL(blob) document.body.appendChild(img) }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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