Storing an arrayBuffer in a MySQL database using blob and xdevapi in Typescript can be accomplished by converting

I need to save various user files (mostly images) that are uploaded via a HTML input field into a MySQL database. My goal is to store the arrayBuffer of these files in a MySQL blob using @mysql/xdevapi.

Current setup:

  • Using Angular with Electronjs
  • MySQL database accessible through my app with @mysql/xdevapi

Progress so far:

Retrieving the input file:

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

Executing MySQL query (updating columns "user_comment" and "user_file" in 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();
    })
}

After downloading the blob in MySQL Workbench, the file size appears to be only 2kb while the original is 165kb. What could be causing this issue? Additionally, how can I retrieve the file for user download?

Answer №1

After some troubleshooting, I was able to solve the issue on my own by transforming the ArrayBuffer into a Buffer like so:

const fileBuffer = Buffer.from(fileToStore);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered when attempting to create a MySQLi table with automatic ID increment functionality

I am encountering a syntax error while trying to create a MYSQLi table using PHP code. <?php require_once('../connect.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "CREATE TABLE email_list ( ...

Parameters for constructing classes in TypeScript

I've been exploring different coding styles in TypeScript recently. When it comes to initializing an object from a class, what are the advantages and disadvantages of these two code styles in TypeScript? class Class3 { // members private rea ...

Getting rows with the highest value from columns when two or more columns have the same value involves identifying the highest value among

I have a posts table and I am trying to display the most viewed posts first. However, I am encountering an issue where if two or more columns have the same value, only one result is being displayed. For example, if one post has 25 views and another post al ...

Converting SQL code to SQLAlchemy mappings

I have an unusual query that aims to retrieve all items from a parent table that do not have corresponding matches in its child table. If possible, I would like to convert this into an SQLAlchemy query. However, I am unsure of how to proceed as my experie ...

Creating an Ionic v4 alert box that redirects users to different pages

I am facing an issue with my ion-alert component where I have set up a message with two options, "To Myself" and "To Someone", that should act like buttons and route to different pages in the application. However, using (click) events or [routerLink] on th ...

Is it possible to create a data structure that enforces specific keys and values types during initialization?

When styling react components with MaterialUI's sx property, I've found that keeping the full style definition inline can lead to cluttered and overwhelming component bodies. To combat this, I've moved all the style definitions into a consta ...

Troubleshooting Node.js and Angular: Resolving the "Cannot Get"

I've successfully retrieved data from Angular and can store it in my local database without any issues. However, when I check the backend server in the web browser, I'm seeing the error message below: Cannot GET Even though the server is rece ...

Converting TimeStamp date and time values into MySQL format in Android Studio

I am facing an issue in trying to insert the value of DatePickerDialog's EditText "etDate" into a MySQL database. The datePickerDialog functions correctly (I can open it, select a date, and display it in the EditText). Below is how I declared it (The ...

Executing a Dynamic SQL join query within django

Consider these simplified classes I am currently working with: class User(AbstractBaseUser): email = models.EmailField() name = models.CharField() is_admin = models.BooleanField() phone_number = models.CharField() class Accounts(model ...

Ways to trigger a function in Angular every 10 seconds

What is the method to utilize Observable function for fetching data from server every 10 seconds? Custom App service fetchDevices (): Observable<Device[]> { return this.http.get(this.deviceUrl) .map(this.extractData) .catch(this ...

Struggling to modify a form element with PHP on a single-page application

As a beginner, I am currently working on creating a signUp form using php and bootstrap. In my mysql database table named 'user_accounts', I have selected the 'emailAddress' of the users as the primary key. My goal is to add a user&apos ...

What could be causing the error message "Type 'Date' is not compatible with type 'string | number'" to appear when trying to create a date using a Date object?

My function is designed to accept a date object as input and return a new date. function makeDate(date:Date) { return new Date(date); //<--error here } const newDate = new Date(); // console.log(makeDate(newDate)); // Returns date object just fine ...

The endpoint throws an error, yet the HTTP subscription fails to capture it

My code seems simple enough. let body = new FormData(); body.set("a", true); body.set("username", this.user); body.set("password", this.password); let headers = new HttpHeaders(); headers.set("Content-Type", 'application/x-www-form-urlencoded') ...

ngOnChanges fails to trigger

I have set up a StackBlitz example (without server side code) which demonstrates an issue with reassigning a variable asynchronously from the backend. I need to update these values so that all objects in the array are of the same type for use in a select ...

The integration of reduce and scan with a specific focus

RxJS version: 5.5.2 Working with an array const v = [1, 2, 3]; The goal is to convert this array into a Subject initially acting like an Observable until all values (1, 2, 3) are consumed. After that, it should behave like a Subject. The challenge lies ...

What are the best ways to maximize the efficiency of an SQL select statement

I'm encountering an issue with a SQL select statement where I'm attempting to calculate percentages using the following logic. For instance, there are two tables involved - TableA and TableB. In TableA, there's a column named ID along w ...

Discovering the width of React Components

Can you help me retrieve the width of the letter "g" and the width of the word "text" so I can use it for future calculations? const CustomizedAxisTick: React.FunctionComponent<ICustomTickProps> = ({x,y,payload,width,fill}) => { const fi ...

Encountered 'DatePickerProps<unknown>' error while attempting to develop a custom component using Material-UI and react-hook-form

Currently, I'm attempting to create a reusable component using MUI Datepicker and React Hook Form However, the parent component is throwing an error Type '{ control: Control<FieldValues, object>; name: string; }' is missing the follow ...

Markers in prepared statements do not function as expected

$dbh = new PDO('mysql:host=' . $_POST['db_host'], $_POST['db_user'], $_POST['db_user_password']); $sql = 'CREATE DATABASE :db_name'; $sth = $dbh->prepare($sql); $sth->bindParam(':db_name', $ ...

The data in Angular2 service is not being saved consistently

I'm diving into Angular for the first time and following along with this tutorial. One of the key features of my Angular app is the CartService, which handles my shopping cart, while the CartComponent displays it in the navbar, and the CartReviewComp ...