0

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 :

  • Angular with Electronjs
  • MySQL database accessible from my app thanks to @mysql/xdevapi

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 ?

1 Answer 1

0

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

let file = Buffer.from(fileToStore);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.