I am creating an app that receives file from several clients in node.js. For that i use express multer node modules.
In the following code I deal with the file upload:
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename){
return filename;
},
onFileUploadStart: function(file){
console.log("file size: " + file.size);
console.log(file.originalname + ' is starting...');
},
onFileUploadComplete: function(file){
console.log(file.fieldname + ' uploaded to ' + file.path);
done = true;
},
onFileUploadData: function(file, data){
//console.log("data being received " + data);
}
}));
But when I enter inside the onFileUploadStart
the file.size
is 0.
A multer file object is a JSON object with the following properties so I know that the size property exists.
fieldname - Field name specified in the form
originalname - Name of the file on the user's computer
name - Renamed file name
encoding - Encoding type of the file
mimetype - Mime type of the file
path - Location of the uploaded file
extension - Extension of the file
size - Size of the file in bytes
truncated - If the file was truncated due to size limitation
buffer - Raw data (is null unless the inMemory option is true)
Why is the size being returned as 0?
EDIT: although the file size returns 0 other properties of the file like file.originalname are working and returning the proper names.
EDIT2: when i print the file inside the onFileUploadStart function i get the following properties and associated values:
{
filename: 'userFile',
originalname: 'name.png',
name: 'name.png',
encoding: '7bit',
mimetype: 'image/png',
path: 'uploads\\name.png',
extension: 'png',
size: 0,
truncated: null,
buffer: null
}
Why is the multer node module sending everything correctly but the file size?
EDIT3:
request.body prints the following:
{ submit: 'upload file' }
which is weird. i am printing the request body inside the following code:
app.post('/upload/', function(request, response){
console.log(request.body);
if (done == true){
response.end('file uploaded');
}
});
index.html file where the client can upload a file:
<html>
<head>
</head>
<body>
<form id= "upload" enctype= "multipart/form-data" action="/upload/" method="post">
<input type="file" name="userFile" />
<input type="submit" name="submit" value="upload file" />
</form>
</body>
</html>