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.
Related
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
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.
I'm using Angular 2 to retrieve image from backend. My response looks like that
{ AcceptRanges: 'bytes',
LastModified: 'Sat, 09 Dec 2017 17:06:46 GMT',
ContentLength: '462882',
ETag: '"c0ff5b060c2da69deff97065d43e3645"',
ContentType: 'application/octet-stream',
Metadata: {},
Body: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 64 00 64 00 00 ff ec 00 11 4475 63 6b 79 00 01 00 04 00 00 00 3c 00 00 ff ee 00 0e 41 64 6f 62 65 00 64 ... > }
Is there any chance I can insert that thing directly inside html img tag, or should I start off by converting it to say base64? Thank you in advance.
I would suggest you to use responseType: ResponseContentType.Blob in your GET-Request settings and convert it later to base64 encoded source. Hope this helps
a solution is to use Base64 Image source, that should return encoded base64 from backend. forExample:
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP//ywAAAAAAQABAAACAUwAOw==";
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.