为了存储base64字符串,我应该使用什么数据类型?

时间:2021-06-01 07:06:34

I have an image with base64 (e.g. data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/7QCcUGhvdG9zaG9w....)

我有一个形象base64(如数据:图像/ jpeg;base64,/ 9 j / 4 aaqskzjrgabagaaaqabaad / 7 qccughvdg9zag9w ....)

how to save in the database? what should be the type of the field in schema? Buffer?

如何在数据库中保存?模式中的字段类型应该是什么?缓冲?

1 个解决方案

#1


2  

The short answer is store as "Binary", for which in a mongoose schema you can use Buffer to do this.

简单的答案是将其存储为“二进制”,在mongoose模式中,您可以使用Buffer来实现这一点。

The longer form is to demonstrate:

较长的形式是演示:

  • Read an image (or any binary data) from file
  • 从文件中读取图像(或任何二进制数据)
  • Base64 encode that data (just to show it can be done)
  • Base64对数据进行编码(只是为了显示它可以完成)
  • Turn back into Binary data from base64 ( just to show it can be done )
  • 从base64转换回二进制数据(只是为了显示它是可以完成的)
  • Store Binary data in the database
  • 在数据库中存储二进制数据
  • Read Binary data from the database
  • 从数据库中读取二进制数据
  • Ouput binary data to a new file
  • 将二进制数据输出到新文件

So the Schema Part is simple, just use Buffer:

所以方案部分很简单,只需要使用Buffer:

var albumnSchema = new Schema({
  name: String,
  image: Buffer
})

Then all be are going to do is follow the process and put the binary data into the property and read it back out again.

然后我们要做的就是跟随这个过程,把二进制数据放到属性中,然后再把它读出来。

Note though that if you are coming directly from a string with a MIME type on it like :

请注意,如果您直接来自一个字符串,上面有一个MIME类型,比如:

  data:image/png;base64,long-String

Just use a javascript .split() and take the second array index for the base64 string itself:

只需使用javascript .split()并获取base64字符串本身的第二个数组索引:

var string = "data:image/png;base64,long-String"
var bindata = new Buffer(string.split(",")[1],"base64");

Here's a listing with a complete demo in and out:

这里有一个完整的演示进进出出的清单:

const async = require('async'),
      mongoose = require('mongoose'),
      Schema = mongoose.Schema,
      fs = require('fs');

mongoose.Promise = global.Promise;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/test');

var albumnSchema = new Schema({
  name: String,
  image: Buffer
})

const Albumn = mongoose.model('Albumn', albumnSchema);


async.series(
  [
    (callback) =>
      async.each(mongoose.models,(model,callback) =>
        model.remove({},callback),callback),

    (callback) =>
      async.waterfall(
        [
          (callback) => fs.readFile('./burger.png', callback),

          (data,callback) => {
            // Convert to Base64 and print out a bit to show it's a string
            let base64 = data.toString('base64');
            console.log(base64.substr(0,200));

            // Feed out string to a buffer and then put it in the database
            let burger = new Buffer(base64, 'base64');
            Albumn.create({
              "title": "burger",
              "image": burger
            },callback)
          },

          // Get from the database
          (albumn,callback) => Albumn.findOne().exec(callback),

          // Show the data record and write out to a new file.
          (albumn,callback) => {
            console.log(albumn);
            fs.writeFile('./output.png', albumn.image, callback)
          }

        ],
        callback
      )


  ],
  (err) => {
    if (err) throw err;
    mongoose.disconnect();
  }
)

And here's a burger.png to play with:

这是一个汉堡。png玩:

为了存储base64字符串,我应该使用什么数据类型?

#1


2  

The short answer is store as "Binary", for which in a mongoose schema you can use Buffer to do this.

简单的答案是将其存储为“二进制”,在mongoose模式中,您可以使用Buffer来实现这一点。

The longer form is to demonstrate:

较长的形式是演示:

  • Read an image (or any binary data) from file
  • 从文件中读取图像(或任何二进制数据)
  • Base64 encode that data (just to show it can be done)
  • Base64对数据进行编码(只是为了显示它可以完成)
  • Turn back into Binary data from base64 ( just to show it can be done )
  • 从base64转换回二进制数据(只是为了显示它是可以完成的)
  • Store Binary data in the database
  • 在数据库中存储二进制数据
  • Read Binary data from the database
  • 从数据库中读取二进制数据
  • Ouput binary data to a new file
  • 将二进制数据输出到新文件

So the Schema Part is simple, just use Buffer:

所以方案部分很简单,只需要使用Buffer:

var albumnSchema = new Schema({
  name: String,
  image: Buffer
})

Then all be are going to do is follow the process and put the binary data into the property and read it back out again.

然后我们要做的就是跟随这个过程,把二进制数据放到属性中,然后再把它读出来。

Note though that if you are coming directly from a string with a MIME type on it like :

请注意,如果您直接来自一个字符串,上面有一个MIME类型,比如:

  data:image/png;base64,long-String

Just use a javascript .split() and take the second array index for the base64 string itself:

只需使用javascript .split()并获取base64字符串本身的第二个数组索引:

var string = "data:image/png;base64,long-String"
var bindata = new Buffer(string.split(",")[1],"base64");

Here's a listing with a complete demo in and out:

这里有一个完整的演示进进出出的清单:

const async = require('async'),
      mongoose = require('mongoose'),
      Schema = mongoose.Schema,
      fs = require('fs');

mongoose.Promise = global.Promise;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/test');

var albumnSchema = new Schema({
  name: String,
  image: Buffer
})

const Albumn = mongoose.model('Albumn', albumnSchema);


async.series(
  [
    (callback) =>
      async.each(mongoose.models,(model,callback) =>
        model.remove({},callback),callback),

    (callback) =>
      async.waterfall(
        [
          (callback) => fs.readFile('./burger.png', callback),

          (data,callback) => {
            // Convert to Base64 and print out a bit to show it's a string
            let base64 = data.toString('base64');
            console.log(base64.substr(0,200));

            // Feed out string to a buffer and then put it in the database
            let burger = new Buffer(base64, 'base64');
            Albumn.create({
              "title": "burger",
              "image": burger
            },callback)
          },

          // Get from the database
          (albumn,callback) => Albumn.findOne().exec(callback),

          // Show the data record and write out to a new file.
          (albumn,callback) => {
            console.log(albumn);
            fs.writeFile('./output.png', albumn.image, callback)
          }

        ],
        callback
      )


  ],
  (err) => {
    if (err) throw err;
    mongoose.disconnect();
  }
)

And here's a burger.png to play with:

这是一个汉堡。png玩:

为了存储base64字符串,我应该使用什么数据类型?