Buffer on URI javascript basis

Asked

Viewed 34 times

1

Well I’m trying to save in my bank Mysql a column in Blob, in the bank he’s saving perfectly like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXEAAACICAMAAAAmsyvzAAAAhFBMVEX///8cY7cAWLMAVrIAWrQAVbIXYbbu8vjI1usAXLQAU7ESX7bb5fL3+v3y9vvA0Ojf5/Pl7PbR3e64yuVmj8mxxeKmvN6Zs9ooarpUg8Ryl82GpdMxb7zM2eyMqdWuwuF7ndA+db9ciMZPgMNDecCVsNiCotKft9t2ms4ubbtsk8sAQKu1P

But when I’m doing the get in my API is returning of the type Buffer, I tried to use some of the things I found on stack but none of them worked for me. The return is like the image:

banco de dados e retorno da api

The function I tried to use was:

  arrayBufferToBase64(buffer) {
    let binary = '';
    let bytes = new Uint8Array(buffer);
    let len = bytes.byteLength;
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
  }

My model using nodejs with sequelize I did so:

// Config
import ApiConfig from '../../config/api.conf';
import { BasicFields } from '../basicFields';

const seq = (sequelize, dataTypes) => {

    // Define environment object
    const basicFields = new BasicFields(dataTypes);
    const config = new ApiConfig();
    const environment = config.getEnv();

    const model = sequelize.define('logo', {
        id: {
            field: "id",
            type: dataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            required: true
        },
        title: {
            field: 'title',
            type: dataTypes.STRING,
            allowNull: false,
            required: false
        },
        base64: {
            field: 'base64',
            type: dataTypes.BLOB('tiny'),
            allowNull: false,
            required: true
        },
        situation: basicFields.setFieldSituation(),

    }, {
            tableName: 'logo',
            paranoid: true,
        }
    );

    return model;

};

module.exports = seq;

Faced with all this, I need the return the same way it is saved in the bank, or in the front (preferential) or in back I need to do this parse to return to string containing "date:image/...."

  • Ever tried to use new Filereader(). readAsText(buffer)? https://developer.mozilla.org/en-US/docs/Web/API/FileReader

  • You can only save Base64 without "data:image/png;Base64,". Done this you can receive this buffer from the bank and convert to Base64 and add the tag later. I think more convenient. You can use .toString('base64').

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.