I develop a web app with option to upload an image to database(sqlite3).
but from some reason the table is not updated after the UPDATE command.
The server is node express.
The table:
CREATE TABLE USER (
id INTEGER PRIMARY KEY AUTOINCREMENT,
isAdmin BOOLEAN,
firstName TEXT,
lastName TEXT,
userName TEXT UNIQUE,
image BLOB,
password TEXT NOT NULL
);
const db = new sqlite3.Database('./database/' + conf.dbName);
const image = .......;
const stmt = 'UPDATE USER SET image=? WHERE id = ?';
const params = [image, 2];
db.run(stmt, params, (error, result) => {
if (error) {
return rej(error.message);
}
return res(result);
});
The image was uploaded from vue and sent to the server with multer.
This is how it looks like:
{
fieldname: 'image',
originalname: '2021-09-04.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 07 80 00 00 04 38 08 06 00 00 00 e8 d3 c1 43 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... 199558 more
bytes>,
size: 199608
}
There is no error from the run function.
I just needed to pass the buffer field of the file object
Related
I have a very complex program in node.js that reads an image, does some modifications on the image and then converts it into buffered data. The buffered image data looks like this.
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 04 38
00 00 04 38 08 06 00 00 00 ec 10var fs = require('fs')
To generate this buffered data, I used:
fs.readFile('image.jpeg', function(err, data){
if (err) throw err
else console.log(data)
}) 6c 8f 00 00 20 00 49 44 41 54 78 5e ec 5d 77 9c 14 45 f6 ... 1001138 more
bytes>
Is there a way to convert this back to an image and save it in my local drive folders using node.js.
Any help is appreciated!
Since you have an image as buffer, you can write the buffer to an image like this (writing only the callback)
function callback(err, data) {
let filename = "test.jpg";
fs.writeFile(filename, data, "binary", (err) => {
if (!err) console.log(`${filename} created successfully!`);
}
}
I'm trying to upload images and videos from my server to Cloudinary but I'm facing lot of issues,
the files in request payload doesn't have a pathname.
Also streamifier doesn't have type declaration so cannot utilise it as well.
For reference,
This is the structure files from payload.
{
fieldname: 'media',
originalname: 'Screenshot 2021-03-25 at 1.42.05 AM.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 03 6c 00 00 00 ba 08 06 00 00 00 d2 e3 ce fe 00 00 0c 62 69 43 43 50 49 43 43 20 50 72 6f 66 69 ... 74947 more bytes>,
size: 74997
}
Answering my own question so according to Cloudinary NodeJS docs, they suggest using streamifier library (https://www.npmjs.com/package/streamifier) , but this library doesn't have declaration library (#types) so I changed the strict property of tsconfig.json to false.
Reference:
https://support.cloudinary.com/hc/en-us/community/posts/360007581379-Correct-way-of-uploading-from-buffer-
It not correct way but for now it works.
I want to read values in the function mv from a json object :
{ name: 'user_step4#upload',
data:
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 00 4c 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01 00 06 ... >,
size: 6841698,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'multipart/form-data',
md5: '226d9791b4e0d637cee207a2f832900e',
mv: [Function: mv] }
Data is a Buffer data type.
Try to convert to string data type.
object.data.toString()
yourJsonObject.data.toString() will convert your buffer to String.
yourJsonObject.mv.toString() will help you to print the function code.
So i've tested my route itself with postman and get the following if I console.log(req.file) I get the following:
{ fieldname: 'myImage',
originalname: '7yFHYXe.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 0c 58 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 0c 48 4c 69 6e 6f 02 10 00 00 ... >,
size: 4760663 }
Which I believe is correct. Now if I test my axios request I get the MulterError: Unexpected field. Here is my axios code
...
export const addPostPicture = uploadData => dispatch => {
var data = new FormData();
data.append("image", uploadData);
console.log(data.get("image"));
axios
.post("/api/uploads/", data, {
headers: {
"Content-Type": `multipart/form-data`
}
})
...
When I console.log(data.get("image")); I get the following:
Forgive my ignorance this is my first time using multer. So any suggestions or feedback is great! =]
Just like all the others with the same error I was dumb and didn't use the input name field accordingly...
So changing data.append("myImage", uploadData); fixed my problem.
I am trying to test amazon web node api sdk for lex for audio input and output and successfully configured it, but when I test it, it sends me garbage inputTranscript(like "yeah", "oh no", "um yeah", etc.) in response.
The request parameters are as below,
var params = {
botAlias: 'Test', /* required */
botName: 'Revert', /* required */
contentType: 'audio/l16; rate=16000; channels=1', /* required */
inputStream: <Buffer 52 49 46 46 80 55 02 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 80 3e 00 00 00 7d 00 00 04 00 10 00 64 61 74 61 54 55 02 00 00 00 00 00 00 00 ... >, /* required */
userId: '12321', /* required */
accept: 'audio/*',
sessionAttributes: {"firstName": "Joe"}
};
The response I am getting as below,
{ contentType: 'audio/mpeg',
sessionAttributes: { firstName: 'Joe' },
message: 'Sorry No Match',
dialogState: 'ElicitIntent',
inputTranscript: 'yeah',
audioStream: <Buffer 49 44 33 04 00 00 00 00 00 23 54 53 53 45 00 00 00 0f 00 00 03 4c 61 76 66 35 37 2e 35 36 2e 31 30 31 00 00 00 00 00 00 00 00 00 00 00 ff f3 60 c4 00 ... > }
For voice recording I am using my laptop inbuilt mic and RecordRTC api as,
recorder = RecordRTC(microphone, {
type: 'audio',
recorderType: StereoAudioRecorder,
desiredSampRate: 16000
});
The recorded voice is encoded into base64 and sent to node server where it is decoded back and sent to lex api in buffer format.