簡體   English   中英

以文本格式顯示文檔文件asp.net c#

[英]to display doc file in text format asp.net c#

我想讀取file。但是這些代碼沒有幫助。

string[] readText = File.ReadAllLines(path); this line is giving error.

protected void btnRead_Click(object sender, EventArgs e)
{
    string path = fileupload1.PostedFile.FileName;
    if (!string.IsNullOrEmpty(path))
    {
        string[] readText = File.ReadAllLines(path);
        StringBuilder strbuild = new StringBuilder();
        foreach (string s in readText)
        {
            strbuild.Append(s);
            strbuild.AppendLine();
        }
        textBoxContents.Text = strbuild.ToString();
    }
}

File.ReadAllText函數期望文件存在於指定位置。 您尚未將其保存在服務器上,但正嘗試讀取它。 如果您不需要將上傳的文件保存在服務器上,則可以直接從輸入流中讀取。

protected void btnRead_Click(object sender, EventArgs e)
{
    if (fileupload1.PostedFile != null && fileupload1.PostedFile.ContentLength > 0)
    {
        using (var reader = new StreamReader(fileupload1.PostedFile.InputStream))
        {
            textBoxContents.Text = reader.ReadToEnd();
        }
    }
}

這將適用於文本文件。 如果要解析其他格式,例如Word文檔,則需要一個庫來完成。

這應該工作

string[] lines = System.IO.File.ReadAllLines(@"..\asd.txt");
for (i = 0; i < lines.Count; i++)
System.Console.WriteLine("Contents = " + lines[i]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM