如何使用 C# .net 读取从 MemoryStream 下载的 CSV 文件?
我正在使用以下代码从外部 URL 下载一个文件,应该是 .csv 文件。
MemoryStream download = new MemoryStream(client.DownloadData(targetUrl));
在下载变量中填充了数据,但我现在的问题实际上是读取该数据。我尝试了以下操作:
StreamReader dataReader = new StreamReader(download,
System.Text.Encoding.Default,
true);
尝试此编码类型和所有其他编码类型仅返回乱码而不是我需要的 .csv 数据。有人能告诉我该怎么做吗?
I'm downloading a file, supposedly a .csv file, from an external URL with the following code.
MemoryStream download = new MemoryStream(client.DownloadData(targetUrl));
In the download variable theres data being populated but my problem now is actually reading that data. I tried the following:
StreamReader dataReader = new StreamReader(download,
System.Text.Encoding.Default,
true);
This and all other Encoding types are tried only return gibberish instead of the .csv data I need. Can anybody tell me how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用默认系统代码页 (
Encoding.Default
) 读取文件数据 - 该文件似乎不采用该编码。您必须使用文件编码所用的编码才能成功读取它。
我建议尝试 Unicode (UTF16)、UTF8 和 ASCII 编码作为可能的选择。如果这些都不能按预期工作(即产生乱码),则需要找出原始编码。
You are reading the file data with the default system code page (
Encoding.Default
) - it appears that the file is not in that encoding.You must use the encoding that the file is encoded with in order to read it successfully.
I suggest trying Unicode (UTF16), UTF8 and ASCII encodings as probable options. If none of these work as expected (that is, gibberish is produced), you need to find out the original encoding.
我会使用 Notepad++ 来找出正在使用的编码。
I would use Notepad++ to find out which encoding is being used.