Error on encoding large file on Node.js side after sending file from Angular-side
Clash Royale CLAN TAG#URR8PPP
Error on encoding large file on Node.js side after sending file from Angular-side
I am getting the file on server side as console.log(body)
.
console.log(body)
fieldname: 'image',
originalname: '21329726_1866651723650020_188839340_o.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './pics/',
filename: '5146c9818ff517c426e34ad84ff3513f',
path: 'pics/5146c9818ff517c426e34ad84ff3513f',
size: 94093
and then encoding by const base64Data = body.buffer.toString("base64")
.
const base64Data = body.buffer.toString("base64")
But for small size files, it works well but for large files it creates a problem. It is not encoding properly.
I think the problem is due to, it starts encoding before it receives the full file.
Please give me some proper way to do this.
This is my GitHub link here.
are you getting full response in console?
– Chellappan
Aug 9 at 9:15
@Endless I want to upload that file in
cloudinary
, and then the url of file in my database.– Rupesh
Aug 9 at 9:16
cloudinary
@Chellappan yes, If checking on angular end. And last half on checking on nodejs
– Rupesh
Aug 9 at 9:17
Still better to upload the file as blob, file or appended into a FormData, you can convert it to base64 on the backend side...
– Endless
Aug 9 at 9:19
2 Answers
2
I think it would be better if you don't convert the pdf file to base64 on the frontend side since it will add request overhead as base64 is bigger in size. Better to upload the file and other request payload using html5 FormData.
Try with these changes.
directly assign the file value to your document property.
this.uploadForm.patchValue(
document: event.target.files[0]
);
//inside your service enrich your data with FormData ,set content-type to undefined and send it
uploadData (uploadForm)
//enrich your data is FormData
let fd= new FormData();
// try looping through `this.form.controls` or uploadForm, to assign it property key and value dynamically
fd.append('branch', branch_value);
fd.append('semester', semester_value);
fd.document('document',document_value)
return this.http.post<RegisterResponse>('/user/upload', fd,set_header_config);
Make sure you set the content-type correctly to undefined. I have tested uploading files in angularjs but not very familier with latest angular changes.
You can check the below blog:
http://learnwebtechs.com/2017/04/22/angularjs-multiple-file-upload-using-http-service
It is sending the file details successfully but when I am encoding it on nodeJs side it encode the file with smaller size, But for large files it is not encoding properly.
– Rupesh
Aug 12 at 13:50
I think for large file before getting the full details, It start encoding.
– Rupesh
Aug 12 at 13:53
I am using
const base64Data = body.buffer.toString("base64");
for encoding.– Rupesh
Aug 12 at 13:53
const base64Data = body.buffer.toString("base64");
I have edited my query. Please check again.
– Rupesh
Aug 12 at 14:04
Please check, I have updated my github link.
– Rupesh
Aug 12 at 22:21
update this code in your /server/routes/user.js
const express = require('express');
const router = express.Router();
// const mongoose = require('mongoose');
const path = require('path');
const multer = require('multer');
var storage = multer.diskStorage(
destination: function (req, file, cb)
cb(null, __basedir + '/upload');
,
filename: function (req, file, cb)
cb(null, file.originalname)
)
const upload = multer(
storage: storage,
limits:
maxFileSize: 1024 * 1024 * 5
).single('filename');
router.post('/upload', upload, (req, res) =>
res.send('uploaded');
);
router.post('/submit', (req, res) =>
console.log(req.body);
);
module.exports = router;
also add below line in app.js
global.__basedir=__dirname;
On which end? angular or node.
– Rupesh
Aug 13 at 9:18
node, inside your 'upload' endpoint.
– Rahul Bisht
Aug 13 at 9:48
do not forget to add 'Content-Type':'application/octet-stream' header from angular post request.
– Rahul Bisht
Aug 13 at 9:49
And then how to fetch
req.file
. inside 'end' function. ?– Rupesh
Aug 13 at 10:01
req.file
It is giving an error
throw new TypeError('Path must be a string. Received ' + inspect(path));
– Rupesh
Aug 13 at 10:06
throw new TypeError('Path must be a string. Received ' + inspect(path));
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why don't you just upload the file as it is instead of converting it to a base64?
– Endless
Aug 9 at 9:14