简体   繁体   English

如何在 MySQL 中将 multer 文件上传为 blob?

[英]How to upload a multer file as a blob in MySQL?

I'm trying to store a file uploaded into a nodejs server into a mysql database.我正在尝试将上传到 nodejs 服务器的文件存储到 mysql 数据库中。 The file is uploaded and processed using a Multer middleware, thus storing all the file information in req.files .该文件使用Multer中间件上传和处理,从而将所有文件信息存储在req.files I believe, correct me if I am mistaken, that this data/parameters must be somehow converted to a blob format yet I do not know how to do so effectively.我相信,如果我弄错了,请纠正我,这些数据/参数必须以某种方式转换为 blob 格式,但我不知道如何有效地这样做。 How could this be done in order to upload it into a MySQL database?为了将其上传到 MySQL 数据库,如何做到这一点?

I am using the latest version of XAMPP with a MySQL InnoDB tables.我正在将最新版本的 XAMPP 与 MySQL InnoDB 表一起使用。 The file parameters/data generated by Multer are shown in the code snippet. Multer 生成的文件参数/数据显示在代码片段中。 I've attempted converting the entire file, that is req.files[0] , into a blob yet the blobs stored in the MySQL database are identical in size despite using different files.我尝试将整个文件(即req.files[0] )转换为 blob,尽管使用不同的文件,但存储在 MySQL 数据库中的 blob 大小相同。

[ { fieldname: 'files',
    originalname: 'Event Information 2018-1_2084.pdf',
    encoding: '7bit',
    mimetype: 'application/pdf',
    destination:
     '/home/millana/Desktop/Brandink/Code/server/middleware/../designs/',
    filename: 'Event Information 2018-1_2084_1559843587360.pdf',
    path:
     '/home/millana/Desktop/Brandink/Code/server/designs/Event Information 2018-1_2084_1559843587360.pdf',
    size: 1125992 } ]

Below is shown the restAPI called to execute the query on an already existing table Designs and database.下面显示了调用 restAPI 以在现有表Designs和数据库上执行查询。

router.post('/order', upload.array('files'), (req, res) => {
    console.log(req.files[0].buffer);

    const order_id = 1;
    const position = 'front';

    const sql = `INSERT INTO Designs VALUES ('${order_id}', '${position}', '${req.files[0].filename}', '${req.files[0].mimetype}', '${req.files[0].buffer}')`;


    db.query(sql, (err, result) => {
        if(err) res.status(400).send(err);
        res.status(200).send(result);
    });

});

The middleware function includes:中间件功能包括:

const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null,  __dirname + '/../designs/');
    },
    filename: function (req, file, cb) {
        let format = '';

        if      ( file.mimetype === 'image/jpeg' )      format = '.jpg';
        else if ( file.mimetype === 'image/png' )       format = '.png';
        else if ( file.mimetype === 'application/pdf' ) format = '.pdf';


        let fileName = _.split(file.originalname, '.')[0] + '_' + Date.now() + format;
        cb(null, fileName);
    }
});

const upload = multer({
    storage: storage,
    limits: {
        fileSize: MAX_SIZE
    },
});

I expect the blob size to vary from one file to another proportionally with their file size.我希望 blob 大小随文件大小成比例地从一个文件变化到另一个文件。 However, the size for all files uploaded is of 9 bytes while the buffers are identical, indicating that the same object/file is being stored.但是,上传的所有文件的大小为 9 字节,而缓冲区相同,这表明正在存储相同的对象/文件。 How can I store each different file accordingly?如何相应地存储每个不同的文件?

How about using fileFilter?使用文件过滤器怎么样? You can manage the quality of file, or size etc. https://github.com/expressjs/multer您可以管理文件的质量或大小等。 https://github.com/expressjs/multer

As you see in links or your knowledge, multer has some options like fileFilter or limits.正如您在链接或知识中看到的,multer 有一些选项,如 fileFilter 或限制。 fileFilter, is similar to your function, to select file type. fileFilter,类似于您的功能,用于选择文件类型。 limits,限制,

fileSize : { 5 * 1024 * 1024 }文件大小:{ 5 * 1024 * 1024 }

And I've heard that storing files like image is to convert a binary file in a buffer.而且我听说存储像图像这样的文件是在缓冲区中转换二进制文件。

if you console.log(img file), you can see the output such as Binary { _bsontype: 'Binary', sub_type: 0, position: 260547, buffer: } }如果你使用console.log(img file),你可以看到输出如Binary { _bsontype: 'Binary', sub_type: 0, position: 260547, buffer: } }

then change array Buffer to base64, string.然后将数组缓冲区更改为 base64,字符串。 the result is,结果是,
data : image/jpeg;base64, v9emlk3SGF45fg...数据:图像/jpeg;base64,v9emlk3SGF45fg...

It is different from 'filename or originalName',它不同于“文件名或原始名称”,

so it could be saved as a different one.所以它可以保存为不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM