Nodejs express multer file upload + path contains double slashes - node.js

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

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'

multipart file upload not working using multer's diskStorage

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?

Multer Doesn't Save Images in Local Folder

Multer cannot store the file, where the destination is in public/my-uploads
const express = require('express');
const app = express();
const multer = require('multer');
let storage = multer.diskStorage({
destination: '/public/my-uploads',
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
const upload = multer({dest:'storage/'}).single('file');
app.post('/upload', upload, (req , res) => {
console.log(req.files) // this does log the uploaded image data.
})
Try this File Storage For Save image in Local
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(
null,
new Date().toISOString().replace(/:/g, "-") + "-" + file.originalname
);
},
});
This happens because you are using windowsOS and where you learned it may be using macOS in windows Path have to setted as mentioned in the above code.
You simply set the file name as given in my answer and destination is root dir "./images"...

How can I upload a zip file using nodejs and extract it?

I want to upload a zip file into the server using node.So can any one help me to figure it out.
First upload your zip file using Multer:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })
Then unzip it using unzipper module:
1) Install unzipper module
npm i unzipper
2) ExtractZip.js JavaScript
const unzipper = require('./unzip');
var fs = require('fs');
fs.createReadStream('path/to/archive.zip')
.pipe(unzipper.Parse())
.on('entry', function (entry) {
const fileName = entry.path;
const type = entry.type; // 'Directory' or 'File'
const size = entry.vars.uncompressedSize; // There is also compressedSize;
if (fileName === "this IS the file I'm looking for") {
entry.pipe(fs.createWriteStream('output/path'));
} else {
entry.autodrain();
}
});
// Source
Test:
c:\Samim>node ExtractZip.js
You can try multer
npm install --save multer
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })

Send Image to Server Nodejs

I want to pass an image from the app to the server. But when I open the picture folder, no image found. I think the image able to send to the server but cannot store the image to the picture folder. I hope you guys can help me solve this problem. Thank you in advance
Project Structure
|
|-db
|-node_modules
|-app.js
|-picture
|-routes
|-perkhidmatan_rumput
|-rumput.js
rumput.js
var router = require('express').Router();
var multer = require('multer');
var upload = multer({ storage: storage })
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null,'../../picture')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
router.post('/api/PostPemantauanPerkhidmatanPotingRumput/:zon/:syarikat/:alamat_syarikat/'+
':nama_penyelia/:taman/:bulan/:tahun/:masa/:timeAMPM/:pusingan/:status/:catatan/:state/'+
':entryOperator', upload.single('image'),(req,res,next) =>{
console.log(req.file)
})
destination :
cb(null,'../../../picture')
and filename will be :
let path = require('path');
cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
Able to solve the problem.
var router = require('express').Router();
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null,'picture/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
var upload = multer({ storage })

Resources