使用Cassandra数据库blob数据显示图像。

时间:2022-10-01 21:26:27

I am storing an image to cassandra db (blob data) and I am trying to get the data and display in my html page,but it is displaying some garbage data.

我正在将一个图像存储到cassandra db (blob数据),并且我正在尝试获取数据并在我的html页面中显示出来,但是它正在显示一些垃圾数据。

I am using cassandra as a database,node js as a backend server and cassandara-client for handling cassandra database.

我使用cassandra作为数据库,使用node js作为后端服务器,使用cassandara-client处理cassandra数据库。

complete description-

完整描述,

I am able to store image to cassandra database.

我可以将图像存储到cassandra数据库。

I am also getting image data from cassandra database,but I don't know in which format the data is?(base64,binary...etc).

我也从cassandra数据库获取图像数据,但是我不知道数据的格式是什么(base64,二进制等等)。

I am also able to write the data into a image file using the below code-

我也能写数据到一个图像文件使用下面的代码-

var fs = require("fs");
fs.writeFile("file1.png", new Buffer(imgData, "base64"), function(err) {});

But I don't want to write data into file .I want to stream the data directly into a image in html.I have used the below code for displaying image in my html page-

但是我不想把数据写进文件,我想用html把数据直接传输到图像中。我使用下面的代码在我的html页面显示图片。

$.ajax({ 
   url: 'http://10.0.0.1:3000/getMap', 
   type: 'GET', 
   headers: {
   "accept": "image/jpg",
            "content-Type": "image/jpg",
    },
   success: function(data) { 
    console.log("Inside success");
    $('#mapdiv').html('<img src="data:image/jpg;base64,'+data+'"  height="200px"  width="500px"/>');
    }
   }); 

<div id="mapdiv"></div>

but i am getting some garbage data inside the div.

但是我在div中得到了一些垃圾数据。

Thanks in advance Subhra

提前谢谢Subhra

2 个解决方案

#1


2  

Your binary content will be stored as an array of bytes in Cassandra with big endianness. In order for your HTML to work, you will need to convert that byte array into a base64-encoded string first.

您的二进制内容将以字节数组的形式存储在Cassandra中,具有很大的机缘性。为了让HTML正常工作,首先需要将字节数组转换为base64编码的字符串。

One thing to watch out for though - depending on the architecture of your machine, you might need to convert the byte array back into little endian format first. That's usually a matter of just reversing the contents of the byte array if you are using little endian.

但是有一件事需要注意——根据您的机器的架构,您可能需要首先将字节数组转换回小的endian格式。如果你使用的是小的endian,那通常只需要反转字节数组的内容。

#2


1  

Cassandra stores data as binary. So, if your write to file code works as expected, I am guessing the data variable in your JS code should really be the base64 encoded value of what you get from Cassandra.

Cassandra将数据存储为二进制。所以,如果你的代码像预期的那样工作,我猜你的JS代码中的数据变量应该是你从Cassandra那里得到的base64编码的值。

#1


2  

Your binary content will be stored as an array of bytes in Cassandra with big endianness. In order for your HTML to work, you will need to convert that byte array into a base64-encoded string first.

您的二进制内容将以字节数组的形式存储在Cassandra中,具有很大的机缘性。为了让HTML正常工作,首先需要将字节数组转换为base64编码的字符串。

One thing to watch out for though - depending on the architecture of your machine, you might need to convert the byte array back into little endian format first. That's usually a matter of just reversing the contents of the byte array if you are using little endian.

但是有一件事需要注意——根据您的机器的架构,您可能需要首先将字节数组转换回小的endian格式。如果你使用的是小的endian,那通常只需要反转字节数组的内容。

#2


1  

Cassandra stores data as binary. So, if your write to file code works as expected, I am guessing the data variable in your JS code should really be the base64 encoded value of what you get from Cassandra.

Cassandra将数据存储为二进制。所以,如果你的代码像预期的那样工作,我猜你的JS代码中的数据变量应该是你从Cassandra那里得到的base64编码的值。