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

I'm trying to parse HTTP Content-Disposition headers using client-side JavaScript. When the filena

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386506a4625463.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信