打字稿:使用BLOB和XDEVAPI将ArrayBuffer存储到MySQL数据库中
我想将用户文件存储(大部分时间图像,但可以是任何东西)用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己想出了自己的工作,将阵列板转换为可行的缓冲区:
I figured it out myself, by converting the ArrayBuffer into a Buffer it works: