multipart file upload not working using multer's diskStorage - node.js

nodejs
I am using multer to upload file in nodejs. The below code seems to work fine.
const upload = multer({ dest: "uploads/" });
But the code below doesn't work.
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads/");
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
},
});
Any idea on how to solve it?

Related

Error no such file or directory, multer package

Getting this error in postman while uploading an image through multer
{
"error": {
"message": "ENOENT: no such file or directory, open 'D:\\adarsh\\API\\uploads\\2022-11-29T17:23:07.574Zdownload1.jpg'"
}
}
Code of it.
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
}
});
const upload = multer({storage: storage});
Tried writing the above code
The problem might be the way you are providing the destination path, try with '/uploads'

File upload via multer with security in uploads folder which is locked

I want to upload files via multer folder and it is working fine
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads");//storage location
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "-" + Date.now() + path.extname(file.originalname));
},
});
var upload = multer({ storage: storage });
var uploadMultiple = upload.fields([{ name: 'data[w_1]', maxCount: 10 }])
But i want to lock the uploads file with password for security issues. How can i upload file via multer to a locked file.
Any suggestion to make my folder more secure are welcome

Error to upload images to the server with NodeJs

I'm tryng to upload images through a form with this code
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/users/')
},
filename: function (req, file, cb) {
cb(null, "user" + Date.now() + file.originalname);
}
});
const upload = multer({ storage: storage });
router.post('/upload-avatar', [md_auth.authenticated, upload.single('file0')], UserController.uploadAvatar);
The problem is wen i try to send it, console throws an error 'md_auth' is not declared or undefined, do you know how t fix that problem?
the variable md_auth on the last line is never defined, causing your error. You must initialize that variable at somepoint in order to use it.

Nodejs express multer file upload + path contains double slashes

I'm trying to upload an image to my directory using Postman. I am using Nodejs and multer as a middleware.
However, I get an ENOENT error:
My question is as follows, why does my code give double \\, and what can I do to change double backslashes to forward slash in the pathname?
My code so far is:
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '.test/');
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
},
});
router.post('/', upload.single('productImage'), (req, res, next) => {
console.log(req.file);
...
...
...
I have tried using the .replace() method without any success.
const multer = require('multer');
let destination = '.uploads/';
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, destination.replace('\\','/'));
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
},
});
I have also tried searching similar posts here on StackOverflow, e.g trying this posts answer Error: ENOENT: no such file or directory,
You can use path.normalize('\\dsgsd\\sdgsdg') method. You can find it in official NodeJS documentation https://nodejs.org/api/path.html#path_path_normalize_path
const multer = require('multer');
const { normalize } = require('path')
let destination = '.uploads/';
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, normalize(destination));
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
},
});
I found the answer after a bit of googling.
The problem was not the double \\, they are allowed, the problem was in the way the filename was saved. The filename was of a datestring, and was saved in the format: 2020-11-25T12:15something, the problem is that Windows OS does not accept files with the character ":".
Solution would be to replace this line of code :
cb(null, new Date().toISOString() + file.originalname);
with
cb(null, new Date().toISOString().replace(/:/g, '-') + file.originalname);
Original answer here

How can I rename uploaded file in Multer with progressive numbers?

I'm uploading images on the server and when I get the images in Express js using Multer, I'm not able to rename them with a progressive number like:
nameImage_1.jpg
nameImage_2.jpg
nameImage_3.jpg
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./assets/img/products/");
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
some ideas ??
THX
I don't know if this will help you but I have it like this
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, __basedir + "/csvs/");
},
filename: (req, file, cb) => {
console.log(file.originalname);
cb(null, `${Date.now()}-name-${file.originalname}`);
},
});

Resources