【问题标题】:Content-Disposition - an invalid character was found in the mail header: '['内容处置 - 在邮件标头中发现无效字符:'['
【发布时间】:2020-08-04 22:41:47
【问题描述】:

我想从下面的 url 中获取文件名。在解析 Content-Disposition 标头以提取文件名时,它会引发错误 System.FormatException: An invalid character was found in the mail header: '['. 。标头的值如下所示。我不想替换这些字符,因为如果我这样做,文件名将被更改。我该如何解决?

Content-Disposition: attachment; filename*=UTF-8''[Saglamindir.net] Internet Download Manager.rar
Url: https://www17.zippyshare.com/d/qS76ZdES/29289/%5bSaglamindir.net%5d%20Internet%20Download%20Manager.rar

我的代码:

var cdHeader = response.Headers["Content-Disposition"];
if (!string.IsNullOrEmpty(cdHeader))
{
    var properFormat = Encoding.UTF8.GetString(Encoding.GetEncoding(1252).GetBytes(cdHeader));
    properFormat = HttpUtility.UrlDecode(properFormat);
    var disp = new ContentDisposition(properFormat);
    var temp = disp.FileName;
    return temp.ReplaceInvalidChars();
}

【问题讨论】:

标签: c# filenames content-disposition formatexception


【解决方案1】:

您不能在此处使用未转义的“[”或“]”(它们需要百分比编码)。相关规范为 RFC 6266 (Content-Disposition) 和 RFC 8187。

【讨论】:

  • 当我尝试不解码 url(使用百分比编码)时,ContentDisposition.FileName 为空。正确的做法是什么?
猜你喜欢
  • 2012-05-07
  • 2015-09-18
  • 2013-07-29
  • 2014-01-03
  • 2014-11-12
  • 2023-02-05
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多