【问题标题】:Is there an easy way to encode (unicode) filename for content-disposition header in Python?是否有一种简单的方法可以在 Python 中为 content-disposition 标头编码(unicode)文件名?
【发布时间】:2020-11-10 19:23:26
【问题描述】:

目前我正在将一个小项目从 node.js/express 转换为 python/falcon。 在节点中编码一个 contect-disposition 标头我正在使用这个库:https://github.com/jshttp/content-disposition

import contentDisposition from 'content-disposition';
resp.setHeader('Content-Disposition', contentDisposition(filename));

正在寻找一种在 Python 中执行此操作的方法。

【问题讨论】:

  • 好的,有一个名为“rfc6266”的库(它似乎总是在谷歌搜索结果的顶部,但由于它的名字我从未注册为答案) - 它应该做同样的事情,但是在我的测试中它仅适用于 Firefox。不适用于 Chrome 和 IE。
  • 由于某种原因,rfc6266 将标头编码为字节,然后将其字符串化为 b"attachment; filename*=utf-8''...",Firefox 仍然可以以某种方式识别。将字节转换为字符串即可解决问题。

标签: python http-headers content-disposition


【解决方案1】:

等效的库称为“rfc6266”(它似乎总是在 google 搜索结果的顶部,但由于它的名称,我从未真正看过该页面,以为它是标准描述)。

import rfc6266
resp.set_header('Content-Disposition', rfc6266.build_header(filename).decode('iso-8859-1')) # the function returns bytes

【讨论】:

  • "rfc6266" 库似乎已被放弃(尽管目前对我有用)。有没有其他办法?
猜你喜欢
  • 2011-02-02
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
相关资源
最近更新 更多