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

1
  • 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. Commented Nov 25, 2018 at 22:57

1 Answer 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.

Sign up to request clarification or add additional context in comments.

13 Comments

The file object or files array are plain javascript objects. You can set new properties directly on them. Does this solution works for you?
@SadiqMustaphaAji Alternatively you can create your own objects from them and manipulate those directly
Please can you provide example on how to create the object. @devconcept
@SadiqMustaphaAji One thing first, are you aware that changing those properties has no effect on your database structure. Did you intend to persist those fields in the database?
Yes @devconcept
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.