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.

originalname is not properly decoded

See original GitHub issue

If uploaded file name contains special characters, the originalname will be set in encoded form.

Steps to repro:

  • upload file named ‘file".ext’
  • expected originalname = ‘file".ext’
  • actual originalname = ‘file%22.ext’

Additional info from browser side:

Headers:
method: POST
content-type: multipart/form-data; boundary=----WebKitFormBoundaryBc2l7WXbILSFIgKL
...
Data:
------WebKitFormBoundaryBc2l7WXbILSFIgKL
Content-Disposition: form-data; name="file"; filename="file%22.ext"
...

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dongherocommented, Oct 1, 2022

this is my solution

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, fullPath);
  },
  filename: function (req, file, cb) {
    let extname = path.extname(file.originalname);
    file.originalname = path.basename(
      Buffer.from(file.originalname, "latin1").toString("utf8"),
      extname
    );
    const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
    filename = file.fieldname + "-" + uniqueSuffix + extname;
    cb(null, filename);
  }
});

the important part is that Buffer.from(file.originalname, "latin1").toString("utf8")

0reactions
LinusUcommented, Oct 11, 2022

@donghero that’s great! From my understanding it seems like HTTP headers are latin1 unless otherwise specified

ref: https://www.jmix.io/blog/utf-8-in-http-headers/

It would probably be nice to add support for filename* as well though

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve damaged document error when opening PDF files
Workarounds to open PDF files that do not open in Adobe Acrobat after the 10.1.5 and 11.0.01 updates.
Read more >
Can't decode accent properly in pandas dataframe
Can't decode accent properly in pandas dataframe ... returns 'TAfAPlzer' , not 'Tolzer' for the original name "Tölzer".
Read more >
Topic: MS Edge takes over pdfs @ AskWoody
Sorry to hear that did not keep your pdfs from being taken over by Edge. ... cursor at the end of the highlighted...
Read more >
(FAQ) Why won't VEGAS open my file correctly? Codecs ...
Many game capture and screen recording programs generate video that does not decode smoothly in VEGAS. Some of them use non-standard codecs ...
Read more >
POST method uploads - Manual - PHP
$uploaddir = './'; is because if you have no coded file constraints a nerd could upload a php script with the same 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