I am sendind file-details from angular to my app.js as
onFileSelected(event: EventEmitter<File[]>) {
const file: File = event[0];
console.log(file);
const formData = new FormData();
formData.append('file', file);
const r = new XMLHttpRequest();
r.open('POST', '/user/upload');
r.send(formData);
}
then in app.js
const multer = require('multer');
const upload = multer({dest:'./pics/'});
router.post('/upload', upload.single('image'), (req,res) => {
const body = req.file;
console.log(body);
const base64Data = new Buffer(JSON.stringify(body)).toString("base64");
console.log(base64Data);
}
and my console.log(body)
gives
{ fieldname: 'image',
originalname: '21329726_1866651723650020_188839340_o.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './pics/',
filename: '5146c9818ff517c426e34ad84ff3513f',
path: 'pics/5146c9818ff517c426e34ad84ff3513f',
size: 94093
}
Now the problem is here -
1 - I don't want to upload my image/pdf
file in any folder, But it is uploading in './pics/'
.
2- I want to upload that file in cloudinary
thats why I want to generate base64
of that file but when I am generating base64
and and uploading that in cloud, it gives an error.
I think that it is not the correct method for encoding in base64
OR I am encoding wrong data format.
Please help me.
By providing the options object (in this case {dest:'./pics/'}
), you're telling multer
that you want to store the files in that directory. Instead, configure it to hold the files in memory:
var storage = multer.memoryStorage()
var upload = multer({ storage: storage })
According to the docs, the file
object should also include a buffer
property, which contains the file data. You should be able to do:
console.log(body.buffer.toString("base64"));