最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to parse Content-Disposition headers that contain UTF-8 characters - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Mar 21, 2018 at 17:56 Sacha 89310 silver badges29 bronze badges asked Mar 21, 2018 at 16:51 rpdrpd 353 silver badges5 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论