打字稿:使用BLOB和XDEVAPI将ArrayBuffer存储到MySQL数据库中

发布于 2025-02-05 11:44:01 字数 1149 浏览 4 评论 0原文

我想将用户文件存储(大部分时间图像,但可以是任何东西)用HTML输入检索到MySQL数据库中。 因此,我想使用 @mysql/xdevapi将文件的arraybuffer存储在mysql blob中,

我正在使用的是:

  • 与electronjs
  • mysql数据库可从我的应用程序访问,请访问 @mysql/xdevapi

我所做的事情:

获取输入文件:

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

mySQL查询(在这里我想从“ step”表中更新列“ user_comment”和“ user_file”表:

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();
    })
}

当我在mysql workbench中下载blob时,我会得到2kb文件,而我的初始文件大小165 kb是我做什么的?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2025-02-12 11:44:01

我自己想出了自己的工作,将阵列板转换为可行的缓冲区:

let file = Buffer.from(fileToStore);

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

let file = Buffer.from(fileToStore);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文