mysqlangulartypescriptelectronmysql-x-devapi

Typescript : store an arrayBuffer into MySQL database using blob and xdevapi


I want to store user files (images most of the time, but it can be anything) retrieved with a HTML input, into MySQL database. So I want to store the arrayBuffer of a file in a MySQL blob, using @mysql/xdevapi

What I'm using :

What I've done :

Get the input file :

processInputFile(event) {
var file: File = event.target.files[0];
file.arrayBuffer().then((value) => {
  this.fileToStore = value;
});

MySQL query (here I want to update column "user_comment" and "user_file" from the "step" table :

UpdateStep = (step: TestSteps, fileToStore: ArrayBuffer): Promise<void> => {
    return new Promise<void>((resolve, reject) => {

        if (this.session === undefined) {
            reject(new Error('session is undefined'));
            return;
        }
        var sch = this.session.getSchema("gtb");
        var step_table = sch.getTable('step');
        step_table.update().set('user_comment', step.user_comment).set('user_file', fileToStore).where('id = ' + step.id).execute().catch(error => { console.error(error); });
        resolve();
    })
}

When I download the blob in MySQL Workbench I get a 2kb file whereas my initial file size is 165 kb. What am I doing wrong ? And also how I can get the file back to let the user download it ?


Solution

  • I figured it out myself, by converting the ArrayBuffer into a Buffer it works:

    let file = Buffer.from(fileToStore);