我想發佈文章在網站上的Word作爲html,我有一個Windows客戶端,將文章轉換爲HTML並將HTML文件發送到網站上的文件夾,我然後在IFrame中顯示文章。但在IE9中,圖像不會顯示,因爲IE9會嘗試將它們轉換爲矢量圖形。我決定從負責這個的HTML中刪除代碼,這裏開始我的問題。在我修改並保存文件後,我收到了垃圾字符,這些字符也顯示在網頁上。但是,如果我手動編輯記事本++中的文件,我不會得到相同的問題,我怎樣才能讀取保存在word中的文件作爲使用C#的HTML,而不是得到這些垃圾字符?這裏是我的代碼C#文件流無法正確讀取word的文字
private bool AdjustHtmlPageForIE9Images(FileInfo file)
{
bool success = true;
try
{
string content = File.ReadAllText(file.FullName);
//replace [if gte vml 1] with [if gte iesucksopd 1]
content = content.Replace("[if gte vml 1]", "[if gte iesucksopd 1]");
//replace [if !vml] with [if !iesucksopd]
content = content.Replace("[if !vml]", "[if !iesucksopd]");
//now write the file over
File.WriteAllText(file.FullName, content);
}
catch (Exception ex)
{
throw ex;
}
return success;
}
,這會導致顯示一些垃圾字符。
嗨,大家好感謝這裏所有的答覆是我做過什麼來解決這個
嗨,大家好感謝您的答覆finaly得到它去,我在FF打開查看編碼,它是西方Windows的1252,那麼在SLaks sed中傳遞GetEncoding(1252)的讀寫操作就是修改後的代碼。
private bool AdjustHtmlPageForIE9Images(FileInfo file)
{
bool success = true;
try
{
Encoding encoding = Encoding.GetEncoding(1252);
string content = File.ReadAllText(file.FullName,encoding);
//replace [if gte vml 1] with [if gte iesucksopd 1]
content = content.Replace("[if gte vml 1]", "[if gte iesucksopd 1]");
//replace [if !vml] with [if !iesucksopd]
content = content.Replace("[if !vml]", "[if !iesucksopd]");
//now write the file over
File.WriteAllText(file.FullName, content, encoding);
}
catch (Exception ex)
{
throw ex;
}
return success;
}
是不是隻是可笑的是IE9不能做這樣一個簡單的事情顯示HTML代碼字在iframe難怪它的受歡迎程度持續下降。
永遠不要寫'拋出'。 – SLaks 2012-01-30 23:33:03
該文件是什麼編碼? – SLaks 2012-01-30 23:33:26
如何找出文件編碼? – smoothe 2012-01-30 23:44:20