【发布时间】: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();
}
【问题讨论】:
-
指定 Content-Disposition 格式(ietf.org/rfc/rfc1806.txt 和 ietf.org/rfc/rfc2183.txt)的 RFC 都不禁止该字符,并且它在 Windows 文件名中是合法的。这是一个奇怪的文件名,但看起来合法
标签: c# filenames content-disposition formatexception