NodeJS mySQL Insert Blob

Try replacing:

var query ="INSERT INTO `files` (`file_type`, `file_size`, `file`) VALUES ('img', " + getFilesizeInBytes(temp_path) + ",'" + buffer + "' );";
mySQLconnection.query(query, function (er, da) {

with:

var query = "INSERT INTO `files` SET ?",
    values = {
      file_type: 'img',
      file_size: buffer.length,
      file: buffer
    };
mySQLconnection.query(query, values, function (er, da) {

You may also want to change file: buffer to file: buffer.slice(0, 100) since you are only reading the first 100 bytes of the file. If buffer.length > 100 then you may end up with a bunch of extra garbage bytes after the first 100 bytes in buffer.


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

    });
});