我正在使用FileUpload服务器控件上传以前从MS Word保存(作为网页;已过滤)的HTML文档.charset是windows-1252.该文档具有智能引号(卷曲)以及常规引号.它还有一些空白(显然),当看起来很深的是正常的TAB或SPACE之外的字符.
在StreamReader中捕获文件内容时,这些特殊字符将转换为问号.我假设它是因为默认的encoidng是UTF-8而且文件是Unicode.
我继续使用Unicode编码创建StreamReader,然后用正确的字符替换所有不需要的字符(我实际在stackoverflow中找到的代码).这似乎工作....只是我不能将字符串转换回UTF-8以在asp:literal中显示它.代码在那里,它应该工作....但输出(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', …Run Code Online (Sandbox Code Playgroud)