With fileupload we can get tempfilepath but with multer any way to get tempfile path?I want to upload files to cloudinary using temp file path.. Here is my code.
const storage = multer.diskStorage({
destination: (req, file, cb)=>{
cb(null, 'uploads')
},
filename: (req, file, cb)=>{
cb(null, Date.now() + file.originalname)
},
useTempFiles: true
})
const upload = multer({storage});
app.post("/upload", upload.single('photo'), (req, res, next)=>{
let fileinfo = req.file;
console.log(fileinfo);
cloudinary.uploader.upload(fileinfo.tempFilePath,{width: 70, height: 90, crop: "fit"}, (err,result)=>{
console.error(err);
console.log(result.url);
});
})
const storage = multer.diskStorage({
filename: (req, file, cb)=>{
cb(null, Date.now() + file.originalname)
}
})
const upload = multer({storage});
app.post("/upload", upload.single('photo'), (req, res,
next)=>{
let fileinfo = req.file;
console.log(fileinfo);
cloudinary.uploader.upload(fileinfo.path,{width: 70,
height: 90,crop: "fit"}, (err,result)=>{
console.error(err);
console.log(result.url);
});
})
Make this changes to your code and try this
Multer gives you access to the uploaded files before storing them.
If you want, however, to store files and access them like you do in PHP, you should use the DiskStorage option of the 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())
}
})
Both the arguments taken by the diskStorage are functions. One function determines the upload folder and the other one the file name.
Related
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"...
I'm new in using nodejs and multer and I want to upload an image but in two different directories. I tried using two different middleware but since the first multer function returns a file destination I couldnt use it to upload in the other multer function. Is it possbile to upload a file using multer in two different directories?
Create multiple storages and call them at the same time.
Example:
const app = require("express")();
const multer = require('multer');
const storageA = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './storageA/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const storageB = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './storageB/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const destA = multer({ storage: storageA });
const destB = multer({ storage: storageB });
function fileUpload(req, res, next) {
destA.single('file')(req, res, next);
destB.single('file')(req, res, next);
}
app.post("/", fileUpload, (req, res) => {
res.json({ file: req.file });
});
app.listen(3000, () => {
console.log("Server started");
});
The uploaded file will be store in ./storageA and ./storageB.
This is not an official way, but went I try it, it works!
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}`);
},
});
Iam using busboy body parser to parse the multipart form data. So Iam getting the fields like name, username key values in req.body and files in req.files. But Iam unable to upload files to the directory using multer.
app.use(busboyBodyParser());
app.post('/createUser', (req, res) => {
console.log(req.body);
console.log(req.files);
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() +
path.extname(file.originalname));
}
});
const upload = multer({
storage: storage
}).single(req.files['uploadpic']);
upload(req, res, (err) => {
});
});
You don't need to use busboy with multer as it is built on top of it.
Your files are not being saved because your files come in an array form but you're trying to read it in as a single file also with a wrong syntax.
upload.single('fieldName') //is the syntax for single file
Your code will be rewritten like this:
//removing busbodyparser
//declaring multer configs
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() +
path.extname(file.originalname));
}
});
const upload = multer({
storage: storage
}).array('uploadpic', 8]
//make sure you define maxCount - here 8 is given as an example
//array will take in a number of files with the same 'uploadpic' field name
app.post('/createUser', (req, res) => {
upload(req, res, function(err) {
if (err) {
//show error
}
//your files are saved and req.body and req.files are populated using multer
console.log(req.files);
console.log(req.body);
});
});
I'm using "multer": "^1.0.6", And i Want to save image in upload folder.
My code is
app.post('/post', multer({dest: './uploads/'}).single('file'), function (req, res) {
response = {
message: 'File uploaded successfully',
filename: req.file.filename
};
res.end(JSON.stringify(response));
});
But I Have the file with this name in upload folder 8e6e425f8756e0bafb40ed1a3cb86964
Why I have this name without mimetype?
Multer saves files without extensions you can read this on GitHub:
filename is used to determine what the file should be named inside the folder. If no filename is given, each file will be given a random name that doesn't include any file extension.
If you want to save with the extension that you write your code like here:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.originalname); // modified here or user file.mimetype
}
})
var upload = multer({ storage: storage })
All information you can find here https://github.com/expressjs/multer/blob/master/README.md
Multer not worried about the extension of the file and leave it completely on your side: you have to define itself a function that will do it. For example, like this:
var multer = require('multer');
var upload = multer({ storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads');
},
filename: function (req, file, cb) {
var ext = require('path').extname(file.originalname);
ext = ext.length>1 ? ext : "." + require('mime').extension(file.mimetype);
require('crypto').pseudoRandomBytes(16, function (err, raw) {
cb(null, (err ? undefined : raw.toString('hex') ) + ext);
});
}
})});
app.post('/post', upload.single('file'), function (req, res) {
response = {
message: 'File uploaded successfully',
filename: req.file.filename
};
res.end(JSON.stringify(response));
});