我正在使用FileUpload服务器控件上载先前从MS Word保存(作为网页;过滤)的HTML文档。字符集是windows-1252。 该文档具有智能引号(卷曲)以及常规引号。它还具有一些空白区域(显然),当深入查看除了正常TAB或SPACE以外的其他字符。FileUpload服务器控件和Unicode字符
在捕获StreamReader中的文件内容时,这些特殊字符会被转换为问号。我假设它是因为默认的encoidng是UTF-8而文件是Unicode。
我继续使用Unicode编码创建StreamReader,然后用正确的(我实际上在stackoverflow中找到的代码)替换所有不需要的字符。这似乎工作....只是我不能将字符串转换回UTF-8以显示它在asp:文字。 代码在那里,它应该工作....但输出(ConvertToASCII)是不可读的。此外
protected void btnUpload_Click(object sender, EventArgs e)
{
StreamReader sreader;
if (uplSOWDoc.HasFile)
{
try
{
if (uplSOWDoc.PostedFile.ContentType == "text/html" || uplSOWDoc.PostedFile.ContentType == "text/plain")
{
sreader = new StreamReader(uplSOWDoc.FileContent, Encoding.Unicode);
string sowText = sreader.ReadToEnd();
sowLiteral.Text = ConvertToASCII(sowText);
lblUploadResults.Text = "File loaded successfully.";
}
else
lblUploadResults.Text = "Upload failed. Just text or html files are allowed.";
}
catch(Exception ex)
{
lblUploadResults.Text = ex.Message;
}
}
}
private string ConvertToASCII(string source)
{
if (source.IndexOf('\u2013') > -1) source = source.Replace('\u2013', '-');
if (source.IndexOf('\u2014') > -1) source = source.Replace('\u2014', '-');
if (source.IndexOf('\u2015') > -1) source = source.Replace('\u2015', '-');
if (source.IndexOf('\u2017') > -1) source = source.Replace('\u2017', '_');
if (source.IndexOf('\u2018') > -1) source = source.Replace('\u2018', '\'');
if (source.IndexOf('\u2019') > -1) source = source.Replace('\u2019', '\'');
if (source.IndexOf('\u201a') > -1) source = source.Replace('\u201a', ',');
if (source.IndexOf('\u201b') > -1) source = source.Replace('\u201b', '\'');
if (source.IndexOf('\u201c') > -1) source = source.Replace('\u201c', '\"');
if (source.IndexOf('\u201d') > -1) source = source.Replace('\u201d', '\"');
if (source.IndexOf('\u201e') > -1) source = source.Replace('\u201e', '\"');
if (source.IndexOf('\u2026') > -1) source = source.Replace("\u2026", "...");
if (source.IndexOf('\u2032') > -1) source = source.Replace('\u2032', '\'');
if (source.IndexOf('\u2033') > -1) source = source.Replace('\u2033', '\"');
byte[] sourceBytes = Encoding.Unicode.GetBytes(source);
byte[] targetBytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, sourceBytes);
char[] asciiChars = new char[Encoding.ASCII.GetCharCount(targetBytes, 0, targetBytes.Length)];
Encoding.ASCII.GetChars(targetBytes, 0, targetBytes.Length, asciiChars, 0);
string result = new string(asciiChars);
return result;
}
,正如我以前说过,有一些更“透明”字,似乎相当于使Word文档已编号压痕,我不知道如何捕捉:
请看看下面他们的unicode价值取代他们....所以如果你有任何提示,请让我知道。
非常感谢!
谢谢兄弟!!! ...做到了! – allendehl 2011-03-16 17:21:24
不客气。 – 2011-03-16 17:33:47