NodeJS mySQL Insert Blob

后端 未结 4 1143
别跟我提以往
别跟我提以往 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:52

    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.

提交回复
热议问题