我有一個帶有瀏覽和提交按鈕的視圖。當用戶單擊瀏覽時,可以瀏覽.doc或.docx文件,以及何時點擊提交按鈕,所選文件的文本應該填充在同一視圖的文本框中。 以下是我在TextBox中讀取和顯示文本的代碼。如何在Asp.Net MVC3中讀取.doc和.docx文件並在文本框中顯示
string filePath =null,docText=null;
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase Infile = Request.Files[inputTagName];
if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".doc"))
{
filePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(Infile.FileName));
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
Infile.SaveAs(filePath);
}
if (filePath != null)
{
docText = System.IO.File.ReadAllText(filePath);
}
ViewBag.displayTextInTextBox= docText;
}
return View();
下面是我查看代碼
<input type="text" id="test1" name="test" value="@ViewBag.displayTextInTextBox">
其顯示特殊字符(如本ࡱ)而非.DOC/.DOCX文檔中的文本。 我是不是正在讀取文件,或者在我的代碼中存在什麼問題。
如前所述,這是不建議的。 http://support.microsoft.com/kb/257757 Microsoft目前不推薦並不支持從任何無人蔘與的非交互式客戶端應用程序或組件(包括ASP,ASP.NET,DCOM,和NT服務),因爲Office在此環境中運行時可能會出現不穩定的行爲和/或死鎖。 – KMan