Multer gridfs Storage
I wanted to change my filename but when I examine console.log (req.file
) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage
.
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
the result of the console is something like
{ fieldname: 'file',
originalname: ''YOU CAN ALSO BE GREAT' - Elon Musk Motivation - Motivational Video.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
id: 5bfb292c13eec142f6c20fd9,
filename: 'a.mp4',
metadata: null,
bucketName: 'contents',
chunkSize: 261120,
size: 19372377,
md5: '513c6220ef3afff644cf8a6dc4cd9130',
uploadDate: 2018-11-25T22:58:52.625Z,
contentType: 'video/mp4' }
{ fileName: 'a' }
node.js mongodb mongoose multer multer-gridfs-storage
add a comment |
I wanted to change my filename but when I examine console.log (req.file
) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage
.
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
the result of the console is something like
{ fieldname: 'file',
originalname: ''YOU CAN ALSO BE GREAT' - Elon Musk Motivation - Motivational Video.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
id: 5bfb292c13eec142f6c20fd9,
filename: 'a.mp4',
metadata: null,
bucketName: 'contents',
chunkSize: 261120,
size: 19372377,
md5: '513c6220ef3afff644cf8a6dc4cd9130',
uploadDate: 2018-11-25T22:58:52.625Z,
contentType: 'video/mp4' }
{ fileName: 'a' }
node.js mongodb mongoose multer multer-gridfs-storage
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57
add a comment |
I wanted to change my filename but when I examine console.log (req.file
) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage
.
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
the result of the console is something like
{ fieldname: 'file',
originalname: ''YOU CAN ALSO BE GREAT' - Elon Musk Motivation - Motivational Video.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
id: 5bfb292c13eec142f6c20fd9,
filename: 'a.mp4',
metadata: null,
bucketName: 'contents',
chunkSize: 261120,
size: 19372377,
md5: '513c6220ef3afff644cf8a6dc4cd9130',
uploadDate: 2018-11-25T22:58:52.625Z,
contentType: 'video/mp4' }
{ fileName: 'a' }
node.js mongodb mongoose multer multer-gridfs-storage
I wanted to change my filename but when I examine console.log (req.file
) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage
.
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
the result of the console is something like
{ fieldname: 'file',
originalname: ''YOU CAN ALSO BE GREAT' - Elon Musk Motivation - Motivational Video.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
id: 5bfb292c13eec142f6c20fd9,
filename: 'a.mp4',
metadata: null,
bucketName: 'contents',
chunkSize: 261120,
size: 19372377,
md5: '513c6220ef3afff644cf8a6dc4cd9130',
uploadDate: 2018-11-25T22:58:52.625Z,
contentType: 'video/mp4' }
{ fileName: 'a' }
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
node.js mongodb mongoose multer multer-gridfs-storage
node.js mongodb mongoose multer multer-gridfs-storage
edited Nov 28 '18 at 22:47
devconcept
2,92011636
2,92011636
asked Nov 25 '18 at 22:44
Sadiq Mustapha AjiSadiq Mustapha Aji
119
119
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57
add a comment |
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57
add a comment |
1 Answer
1
active
oldest
votes
This part in your code
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => { // In this function is where you configure the name of your file
The file
configuration is the one that computes the filename before inserting the file in the database. What you are doing is:
- Generating a name like
'file'
plus whatever extension comes from the browser, eg:'file.mp4'
- Saving a file with that name into the database
- Overwriting a property in your request with a new name
- The file in the database remains unchanged
I think what you really wanted was to generate the correct name before inserting
You can do this by using
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
// In here you have access to the request and also to the body object
const filename = req.body.fileName + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
Make sure you send all the fields before the file from the form in your browser or some values will be undefined
because they are not processed yet.
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
Thefile
object orfiles
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
|
show 10 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472754%2fmulter-gridfs-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This part in your code
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => { // In this function is where you configure the name of your file
The file
configuration is the one that computes the filename before inserting the file in the database. What you are doing is:
- Generating a name like
'file'
plus whatever extension comes from the browser, eg:'file.mp4'
- Saving a file with that name into the database
- Overwriting a property in your request with a new name
- The file in the database remains unchanged
I think what you really wanted was to generate the correct name before inserting
You can do this by using
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
// In here you have access to the request and also to the body object
const filename = req.body.fileName + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
Make sure you send all the fields before the file from the form in your browser or some values will be undefined
because they are not processed yet.
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
Thefile
object orfiles
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
|
show 10 more comments
This part in your code
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => { // In this function is where you configure the name of your file
The file
configuration is the one that computes the filename before inserting the file in the database. What you are doing is:
- Generating a name like
'file'
plus whatever extension comes from the browser, eg:'file.mp4'
- Saving a file with that name into the database
- Overwriting a property in your request with a new name
- The file in the database remains unchanged
I think what you really wanted was to generate the correct name before inserting
You can do this by using
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
// In here you have access to the request and also to the body object
const filename = req.body.fileName + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
Make sure you send all the fields before the file from the form in your browser or some values will be undefined
because they are not processed yet.
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
Thefile
object orfiles
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
|
show 10 more comments
This part in your code
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => { // In this function is where you configure the name of your file
The file
configuration is the one that computes the filename before inserting the file in the database. What you are doing is:
- Generating a name like
'file'
plus whatever extension comes from the browser, eg:'file.mp4'
- Saving a file with that name into the database
- Overwriting a property in your request with a new name
- The file in the database remains unchanged
I think what you really wanted was to generate the correct name before inserting
You can do this by using
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
// In here you have access to the request and also to the body object
const filename = req.body.fileName + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
Make sure you send all the fields before the file from the form in your browser or some values will be undefined
because they are not processed yet.
This part in your code
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => { // In this function is where you configure the name of your file
The file
configuration is the one that computes the filename before inserting the file in the database. What you are doing is:
- Generating a name like
'file'
plus whatever extension comes from the browser, eg:'file.mp4'
- Saving a file with that name into the database
- Overwriting a property in your request with a new name
- The file in the database remains unchanged
I think what you really wanted was to generate the correct name before inserting
You can do this by using
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
// In here you have access to the request and also to the body object
const filename = req.body.fileName + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
Make sure you send all the fields before the file from the form in your browser or some values will be undefined
because they are not processed yet.
answered Nov 27 '18 at 15:37
devconceptdevconcept
2,92011636
2,92011636
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
Thefile
object orfiles
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
|
show 10 more comments
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
Thefile
object orfiles
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
I'm creating video on demand Api I wanted to upload video and Thumbnail to the DB. a part from default property of the file like {id, field, filename, metadata, bucketname}. I want to add my own custom property like Title, Description, category to the file property so i will endup having {id, field, filename, metadata, bucketname, title, category, description}.
– Sadiq Mustapha Aji
Nov 28 '18 at 21:53
The
file
object or files
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?– devconcept
Nov 28 '18 at 22:02
The
file
object or files
array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?– devconcept
Nov 28 '18 at 22:02
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
– devconcept
Nov 28 '18 at 22:08
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Yes it works thanks
– Sadiq Mustapha Aji
Nov 28 '18 at 22:49
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
Please can you provide example on how to create the object. @devconcept
– Sadiq Mustapha Aji
Nov 28 '18 at 22:50
|
show 10 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472754%2fmulter-gridfs-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to Stackoverflow. Please copy and paste any code you want to display into the question. Best not to use embedded images for code or error messages.
– bcperth
Nov 25 '18 at 22:57