2

在 asp.net 网站上,用户尝试将文件作为电子邮件附件上传,文件名中包含 emdash。当将此作为电子邮件附件(交换服务器)发送时,文件已转换为 _utf8_B_ * ** * .dat

因此,在 .aspx 页面上,我需要能够检测作为 Request.Files 集合的一部分上传的文件的文件名中是否存在 emdash。

string s = "a—b-";

byte[] arr = Encoding.ASCII.GetBytes(s);
foreach (byte element in arr)
{
   Response.Write(element.ToString() + ",");
}

上面的字符串有一个破折号作为第二个字符,一个普通连字符作为第四个字符。

上面的代码将 97,63,97,45 打印到屏幕上。

我假设 emdash 不是一个有效的 ASCII 字符,要么会抛出一个错误,要么有一些迹象表明它不是一个有效的 ASCII 字符。然而它返回 63。

如何检测文件名中的 emdash,以便我可以对用户说“您的文件名中包含无效字符”?我在这个问题上看到了其他问题,我无法让它们工作。

4

2 回答 2

2

How can I detect an emdash in a file name so I can say to the user 'Your file name has an invalid character in it'?

That's the wrong way around, because tomorrow a user will upload a file with another unicode character your filesystem or its API doesn't support. Besides you don't need ASCII, because NTFS can handle a lot more than 7 bytes per character.

The right question is: "What characters can I use to save a file"? But then again you'll be tied to the filesystem implementation. You'd best just generate a random filename and write the file to that path, and store the filename in a database so you can view the original filename.

If you do want to save the file under the user-provided path, you'll have to remove Path.GetInvalidPathChars() and Path.GetInvalidFileNameChars() from your the input.

If the problem is not the filesystem but the mail system, please show relevant code and error message.

于 2013-06-03T13:37:02.853 回答
2

这应该可以解决问题:

    foreach (char c in s) {
        if (c >= 128) {
            Response.Write("Non-ascii char detected: {0}", c);
        }
    }

我相信它Encoding.ASCII.GetBytes首先会转换为 ASCII,所以当你调用它时你不应该看到非 ASCII 字符。

于 2013-06-03T13:34:37.197 回答