我正在使用blazor组件,我希望我的模型从我的示例数据文件夹中读取一个.tsv或.csv文件。我知道我可以用
protected override async Task OnInitializedAsync()
{
fileName = await Http.GetStringAsync("sample-data/randomFile.tsv");
}但是,它会以字符串的形式出现,我不能使用像.ReadAllLines()这样的文件读取器函数。如何才能按原样访问该文件?
编辑:我意识到我可以将它作为字符串传递并解析该字符串。现在的问题是,字符串不能到达模型。
@code {
static string fileName = "this \n is \n a \n test";
protected override async Task OnInitializedAsync()
{
fileName = await Http.GetStringAsync("sample-data/22_AB9_CL_0228.tsv");
}
string[,] table = FileReader.ConvertToMatrix(fileName, "sample-data/randomFile.tsv");
}模型接收“此\n是\n \n测试”
发布于 2022-05-23 04:24:24
StringReader类在System.IO中可以使用CR或CRLF分隔符解析字符串。
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://filesamples.com/samples/document/csv/sample2.csv");
string fileContent = await response.Content.ReadAsStringAsync(); // This method looks at content-type header to find how string is encoded (UTF8, ASCII, ...)
using (StringReader sr = new StringReader(fileContent))
{
string? line = null;
while ((line = sr.ReadLine())!=null)
{
string[] items = line.Split(",");
// Process the items
}
}https://stackoverflow.com/questions/72322217
复制相似问题