admin管理员组文章数量:1296332
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.
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.
Share Improve this question asked Aug 11, 2018 at 10:36 RupeshRupesh 8902 gold badges15 silver badges31 bronze badges2 Answers
Reset to default 7By 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"));
I would suggest you to use multiparty. A simple and easy solution handle form-data.
Here you can upload files without saving it. I'm using this for save files in AWS S3
router.post('/upload',(req,res)=>{
let form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
//here files is array
const base64Data = new Buffer(JSON.stringify(body)).toString("base64");
console.log(base64Data);
});
});
Or else you simply omit the dest
object. The file will be stored in memory instead of a physical location:
本文标签:
版权声明:本文标题:javascript - How to encoded files in base64 in nodejs after getting details from client-side using multer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741635241a2389603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论