![](/img/trans.png)
[英]How do I save a multi-line(fasta format) as one line in textbox (C#)?
[英]how to display the contents of file in multi-line textbox in readable format and in asp.net C#
我正在使用FileUpload Control上传FileUpload.aspx页面中的文件,现在我想在不同的页面中显示文件的内容,例如-filecontents.aspx。 为此我使用StreamReader,甚至以编码格式(不是可读格式)获取内容。
如何以可读格式获取上传文件的内容。
这是我的代码:
public string ShowContent(string path)
{
string savePath = @"D:\CloudStorageSecurity\CloudStorageSecurity\CloudServer1\";
string strInput = "";
string GetStream = "";
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path, Encoding.Unicode);
strInput = sr.ReadLine();
while (strInput != null)
{
GetStream += strInput;
strInput = sr.ReadLine();
}
sr.Close();
}
else
{
Response.Write("file does not exist!");
}
txtFileCOntents.Text = GetStream;
}
考虑到path
是有效路径并指向可读文件格式(txt,html,rtf ...):
public string ShowContent(string path)
{
var result = string.Empty;
if (File.Exists(path))
{
using(var sr = new StreamReader(path, Encoding.UTF8))
{
result = sr.ReadToEnd();
sr.Close();
}
}
else
{
Response.Write("file does not exist!");
}
txtFileCOntents.Text = result ;
}
当你有result
,你可以做任何事情。
如果你想逐行阅读:
public string ShowContent(string path)
{
var result = string.Empty;
var tmpLine = string.Empty;
if (File.Exists(path))
{
using(var sr = new StreamReader(path, Encoding.UTF8))
{
while((tmpLine = sr.ReadLine()) != null)
{
result += tmpLine + "\n\r";
}
}
}
else
{
Response.Write("file does not exist!");
}
txtFileCOntents.Text = result ;
}
这一切都取决于您尝试“阅读”的文件类型。
我在评论中看到你试图解析docx格式。 您可以通过OpenXML获取可读内容。 我想这个链接可以帮助你开始。
https://code.msdn.microsoft.com/office/CSOpenXmlGetPlainText-554918c3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.