Multer gridfs Storage












0















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' }











share|improve this question

























  • 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
















0















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' }











share|improve this question

























  • 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














0












0








0








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' }











share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















1














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:




  1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'

  2. Saving a file with that name into the database

  3. Overwriting a property in your request with a new name

  4. 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.






share|improve this answer
























  • 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











  • @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











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
});


}
});














draft saved

draft discarded


















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









1














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:




  1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'

  2. Saving a file with that name into the database

  3. Overwriting a property in your request with a new name

  4. 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.






share|improve this answer
























  • 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











  • @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
















1














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:




  1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'

  2. Saving a file with that name into the database

  3. Overwriting a property in your request with a new name

  4. 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.






share|improve this answer
























  • 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











  • @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














1












1








1







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:




  1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'

  2. Saving a file with that name into the database

  3. Overwriting a property in your request with a new name

  4. 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.






share|improve this answer













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:




  1. Generating a name like 'file' plus whatever extension comes from the browser, eg: 'file.mp4'

  2. Saving a file with that name into the database

  3. Overwriting a property in your request with a new name

  4. 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.







share|improve this answer












share|improve this answer



share|improve this answer










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











  • 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











  • 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











  • 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











  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen