question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I change filename of the file?

See original GitHub issue

I’d like to change the file name to other that’s send as a property of the data object, here’s the json of the file:

{ fieldname: 'file',
  originalname: 'db.pdf',
  encoding: '7bit',
  mimetype: 'application/pdf',
  destination: './client/public/docs/transactions/',
  filename: '8a3fe7cb7369d9cffa8e17b162ec3d6b',
  path: 'client\\public\\docs\\transactions\\8a3fe7cb7369d9cffa8e17b162ec3d6b',
  size: 9179145 }

Client side upload with ng-file-upload:

Upload
    .upload( {
        url: '/api/v1/upload',
        data: {
            file: vm.pdf,
            new_name: vm.new_pdf_name //This is the name I want for the file
        }
    } )
    .then(
        function ( response ) { ... }
    );

I’m using diskStorage but the request of filename property function doesn’t receive new_file_name property…

var storage = multer.diskStorage(
    {
        destination: './uploads/',
        filename: function ( req, file, cb ) {
            //req.body is empty...
            cb( null, file.originalname );
        }
    }
);

How could I send the new_file_name through that req.body?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
LinusUcommented, Dec 3, 2015

Yes that is the expected behaviour, the filename function is called whenever a file is detected on the wire. It needs to run before the file is actually consumed since it determines where to save it.

Your request from the client looks like this:

<header> | file | new_file_name
           ^ filename function fired

You need to send it as such:

<header> | new_file_name | file
                           ^ filename function fired
5reactions
LinusUcommented, Dec 3, 2015

If req.body is empty, it means that the client sent the file before sending the new_file_name field. There is nothing we can do about it in multer.

This has come up a number of times before, e.g. #146 where the workaround is further outlined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rename a file - Microsoft Support
Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want...
Read more >
Rename a file or folder
To rename a file or folder: · Right-click on the item and select Rename, or select the file and press F2 . ·...
Read more >
How do I rename a file in Windows?
There are many ways to rename a file in Windows. The easiest way is by right-clicking on the file and selecting Rename. You...
Read more >
Rename a file - Files by Google Help
Rename a file · On your Android device, open Files by Google Files by Google · On the bottom, tap Browse Browse ·...
Read more >
6 Ways to Rename Files and Folders in Windows 10
To rename a file from the context menu, right-click a folder, and click “Rename” from the context menu that appears. With the name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found