FileContentResult x23File = File(x23.FileData,
"application/text", x23.Filename);
string[] Lines = System.Text.Encoding.UTF8.GetString(x23File);
答案 0 :(得分:3)
如果我们假设x23.FileData
是byte[]
,您可能需要:
List<string> lines = new List<string>();
using(var ms = new MemoryStream(x23.FileData))
using(var reader = new StreamReader(ms, Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
lines.Add(line);
}
现在lines
包含所有单独的行。请注意,您也可以通过IEnumerable<string>
以非缓冲方式使用此数据。例如:
static IEnumerable<string> ReadLines(byte[] source, Encoding enc = null)
{
using(var ms = new MemoryStream(source))
using(var reader = new StreamReader(ms, enc ?? Encoding.UTF8))
{
string line;
while((line = reader.ReadLine()) != null)
yield return line;
}
}
答案 1 :(得分:0)
您收到该错误是因为x23File
不是byte[]
。你的第二行应该是
string[] Lines = System.Text.Encoding.UTF8.GetString(x23File.FileContents).Split(new String[]{"\r\n"}, StringSplitOptions.None);