I'm trying to parse HTTP Content-Disposition headers using client-side JavaScript. When the filename is made up of ASCII characters, this is easy. But when there are non-ASCII characters involved, the header looks something like this:
Content-Disposition:attachment; filename="john?doe.jpg"; filename*=UTF-8''john%E2%80%93doe.jpg
I can parse this in Node.js by using the content-disposition
npm package. I tried using this package in client-side code, but it fails on the following line because the browser (eg Chrome) doesn't support the Buffer
class:
value = new Buffer(binary, 'binary').toString('utf8')
Can anyone tell me of another way to parse these headers using client-side JavaScript, or a way to replace this usage of Buffer
with something else?
I've looked around for a solution, but every time someone asks how to parse the content-disposition
header, the answers only apply to the ASCII case.
In a web browser, you can use:
decodeURIComponent('john%E2%80%93doe.jpg')
decodeURIComponent is designed for %-encoded bytes, representing text, and assuming the UTF-8 character encoding of the Unicode character set.