After specifying the path for the uploaded image to ./uploads my images are still going towards tmp folder.
My multer configuration
const multer = require("multer");
const storage = multer.diskStorage({
desitnation: function(req, file, cb){
cb(null, './uploads/');
},
filename: function(req, file, cb){
cb(null, file.originalname);
}
})
const upload = multer({storage: storage});
It seems the spelling desitnation is wrongly spelled. It should be destination.
Related
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'
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
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 create a endpoint in node.js (localhost:8000/file) that receives files and stores them somewhere locally.
You can use multer npm module for this
Write a middleware named upload.js in express which will upload your file to your server
upload.js
const multer = require('multer');
const maxSize = 10 * 1024 * 1024;
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
cb(null, file.originalname)
},
});
const upload = multer({
storage: storage
});
module.exports = upload;
This will upload your file to your server.You need to call it in your route
app.js
const upload = require('./upload.js')
router.post("/files", upload.single('file'), (req,res)=>{
console.log("This is file ",req.file)
});
Make a folder named uploads and all files will be in uploads folder.
probably a stupid question but maybe someone here could help me.
after uploading files with multer and express, what would be the url of the uploaded files to uploads/?
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
}
});
if it's an image, how can i link to it like:
http://localhost:3000/uploads/image.jpg?
should i place the uploaded files in the public directory?
thanks
This is how I setup Multer for expressjs. First you have to overwrite the renaming function if you want to keep the original filename. Then you have to move the uploads folder to the public folder.
// this uploads a single input[file] field called 'image'
var express = require('express'),
multer = require('multer'),
upload = multer({
storage: multer.diskStorage({
destination: 'public/images/uploads/',
filename: function(req, file, cb) {
// this overwrites the default multer renaming callback
// and simply saves the file as it is
cb(null, file.originalname)
}
})
}),
router = express.Router()
// add route
router.post('/uploadimage', upload.single('image'), function(req, res, next) {
if (!req.file) return next(new Error('Select a file!'))
// be careful here as the upload path has 'public' at the start
// which is the static mounted directory so doesn't show
// here the path is build manually
var imagePath = '/images/uploads/' + req.file.filename;
res.end('<img src=" + imagePath + " />')
})
additionally you can keep the default upload path as /uploads and mount it as a static folder
// Mount uploads
app.use(express.static(path.resolve('./uploads')));
Make static teh diskstoarge location in your case 'uploads' folder should be static
app.use(express.static('uploads'));
https://expressjs.com/en/starter/static-files.html