NodeJS mySQL Insert Blob

后端 未结 4 1144
别跟我提以往
别跟我提以往 2020-12-20 17:03

I need a little help with NodeJS and MySQL blob insertion.

Here\'s the code snippet i\'m using

fs.open(temp_path, \'r\', function (status, fd) {
             


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 17:45

    Thank you mscdex for the snippet.

    The problem was as you pointed out that i was reading only first 100 bytes of data. BTW thank you for the snippet and here's the whole solution. Hope it can help someone :-)

    fs.open(temp_path, 'r', function (status, fd) {
        if (status) {
            console.log(status.message);
            return;
        }
        var fileSize = getFilesizeInBytes(temp_path);
        var buffer = new Buffer(fileSize);
        fs.read(fd, buffer, 0, fileSize, 0, function (err, num) {
    
            var query = "INSERT INTO files SET ?",
                values = {
                    file_type: 'img',
                    file_size: buffer.length,
                    file: buffer
                };
            mySQLconnection.query(query, values, function (er, da) {
                if(er)throw er;
            });
    
        });
    });
    

提交回复
热议问题