I have simple multer image uploading.
// Multer settings
// STORAGE FOR USER AVATAR
var storageAvatar = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/avatars/')
},
filename: function (req, file, cb) {
cb(null, req.user.id + '.jpg')
}
})
// STORAGE FOR ARTICLE THUMBNAILS
var storageThumbnail = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/thumbnails/')
},
filename: function (req, file, cb) {
cb(null, "clanok" + '.jpg')
}
})
// SETTING UPLOAD FOLDER
var upload = multer({
storage: storageAvatar
})
// Multer BEFORE CSRF!!!
app.use(upload.fields([{
name: 'avatar',
maxCount: 1,
}, {
name: 'thumbnail',
maxCount: 1,
}]));
my problem is that i am unable to set different folder for avatars and thumbnails. I can set only one folder for both :/ Anything else i tried ends with invalid CSRF. Thanks for anny suggestions.
EDIT: in one form i am using only one thing. So for example in profile update i have only possibility to change avatar in article adding i have only ability to change thumbnail of article. They not need to bey in upload fields can be seperate but dont know how.
I found on forum way how to go over csrf problem by adding ?_csrf={{csrfToken}} to the end of action in form so i can use official way of using multer :)
Related
I am trying to make document management project using nodejs,mongodb and react. I want to make directory folder that will store document and the sub is it file. But I could not find any reference that I could use to start making the directory. Is there any specific term that I should search to get any reference to start the project? Thank you.
You can use Multer library for file uploads. It's super easy to use.
import multer from 'multer';
//specify where you want your files to be stored
var fileStorageEngine = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './images')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '--' + file.originalname)
}
})
var upload = multer({ storage: fileStorageEngine });
//pass it as a middleware to a route where you expect to get a file upload
app.use('/api/file/', upload.single('image'), someRouterHere);
So part of my Schema looks like this photo: [{data: Buffer, contentType: String }]
And Multer stores the images like so:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads');
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
});
app.use(multer({
storage: storage
}).single('photo'));
Now I know it stores the images as a Buffer in Mongodb and stores the files on the server too. I would like it stored the path to the file in the database and then have it store the actual image file on the server. It stores the images as text files it seems, encoded with utf-8. This is an awfully complicated process for a seemingly simple task (store images)
You can access the path of the file using the req.file.path
app.post("/file", function(req, res) {
console.log(req.file.path)
})
I'm fairly new to nodejs/ express, but no matter what I seem to do I cant seem to get multer to save to the specified destination, it seems to completely ignore the parameter all together. The code is shown below
//app.js
var multer = require('multer');
var fs = require('fs');
var apiRouter = express.Router();
var app = express();
var store = multer.diskStorage({
filename: function(req,file,cb){
console.log("filename");
cb(null, Date.now()+'.'+file.originalname);
},
desitnation: function(req,file,cb){
console.log("storage");
cb(null,'./public/');
}
});
var upload = multer({storage:store}).single('file');
apiRouter.post('/upload', function(req, res){
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
}
console.log(req.file);
return res.json({originalname:req.file.originalname, uploadname:req.file.filename});
});
});
The response I get when uploading is shown below:
GET /vendor.js.map 200 3.916 ms - 6636755
filename
{ fieldname: 'file',
originalname: 'Desert.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\\Users\\Dwyer\\AppData\\Local\\Temp',
filename: '1538979138829.Desert.jpg',
path:
'C:\\Users\\Dwyer\\AppData\\Local\\Temp\\1538979138829.Desert.jpg',
size: 845941 }
POST /api/upload 200 70.031 ms - 69
It seems to be setting the file correctly, but I'm not sure where it gets the destination from, no3 do I understand why the destination parameter isn't being read.
Its actually "destination" (not desitnation). You should also make sure that you have a folder ,with the specified name, at the specified destination.
Do you try using physical address in destination? I guess destination in multer document is physical address in linux os.
but no matter what I seem to do I cant seem to get multer to save to the specified destination, it seems to completely ignore the parameter all together.
This is because there seem to be a typo in the configuration.
'use strict';
var store = multer.diskStorage({
filename: function (req, file, cb) {
console.log("filename");
cb(null, Date.now() + '.' + file.originalname);
},
destination: function (req, file, cb) { // it is destination not desitnation :)
console.log("storage");
cb(null, './public/');
}
});
I'm using Multer and express to add image via a form. I have successfully created the file in an uploads folder. When I try to call the url it is being downloaded in a file format. I need to open it in a new tab based on mime type.
What is the best way to achieve this?
Below is my code:
app.use('/images', express.static(__dirname + '/file/uploads/'));
Sample url:
http://localhost:2020/images/b78339184694e2e6864a77d4f3067db5
I added filename in my storage variable to achieve the required result.
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, __dirname+'/file/uploads/')
},
filename: function (req, file, cb) {
console.log(file);
var extArray = file.mimetype.split("/");
var extension = extArray[extArray.length - 1];
var name = crypto.randomBytes(10).toString('hex');
cb(null, name + Date.now()+ '.' +extension)
}
});
i am uploading files with multer nodejs. Everything Works fine but when input type="file" contains name="warranty[][warantycopy]" i am unable to access this file.
Below is my code in index.js:
var multer = require('multer');
var invoice_copies = '';
var storage_Copies = multer.diskStorage({
destination: function (req, file, cb) {
// console.log("fileefefef");
// console.log(file);
cb(null, 'uploads/'+file.fieldname)
},
filename: function (req, file, cb) {
invoice_copies=(file.originalname).replace(/ /g,"_")
cb(null, invoice_copies)
}
});
var uploadCopies = multer({ storage: storage_Copies })
post request contains this code
router.post('/vehicle-battery',uploadCopies.fields([{name:'invoice_copy1'},
{name:'warrantyDetails'}]), function(req, res) {
//my code
}
my jade file is :
form#formAddUser(name="addVehicle",method="post",action="/vehicle-battery",enctype="multipart/form-data")
input(type="file", name="invoice_copy1",class="form-control")
input#warrantyCard(type="text", placeholder="warranty Card Number", name="warrantyDetails[0][warrantyCardNumber]",class="form-control")
input#warrantyCardExpiry(type="text", placeholder="warranty Card Expiry in Month", name="warrantyDetails[0][warrantyCardExpiry]",class="form-control")
input#warrantyCardCopy(type="file", placeholder="warranty Card Expiry copy", name="warrantyDetails[0][warrantyCardCopy]",class="form-control")
textarea(placeholder="Particulars" name="battery_paticulars")
button#btnSubmit(type="submit",class="form-control") submit
if i am using
input#warrantyCardCopy(type="file", placeholder="warranty Card Expiry copy", name="warrantyDetails",class="form-control")
this code working fine.
From a comment above:
after working for research for so long time i solved it . I am providing my solution so that it can help someone else. i have used
router.post('/vehicle-battery',uploadCopies.any(), function(req, res) { //my code }