简体   繁体   English

以文本格式显示文档文件asp.net c#

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

i want to read content of file.but these code is not helping. 我想读取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();
    }
}

The File.ReadAllText function expects the file to exist on the specified location. File.ReadAllText函数期望文件存在于指定位置。 You haven't saved it on the server and yet you are attempting to read it. 您尚未将其保存在服务器上,但正尝试读取它。 If you don't need to save the uploaded file on the server you could read directly from the input stream. 如果您不需要将上传的文件保存在服务器上,则可以直接从输入流中读取。

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();
        }
    }
}

This will work for text files. 这将适用于文本文件。 If you want to parse some other formats such as Word documents you will need a library to do that. 如果要解析其他格式,例如Word文档,则需要一个库来完成。

this should work 这应该工作

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