如何在 ASP.NET 网站中下载 CSV 文件
在我的网站中,我尝试下载来自 Yahoo 的 CSV 文件。它包含一些数据。
我正在使用下面给出的代码来下载 CSV。
问题:
我想从 Yahoo 的 CSV 下载并获取所有数据,但我这边并未创建整个 CSV。
仅复制部分数据。因此 CSV 不会连同其所有数据一起下载。
我尝试增加缓冲区大小,但这没有帮助
雅虎 CSV 中的数据如下面的屏幕截图所示。这是我要下载的数据
我在创建的 CSV 中获取的数据,当我下载了相同的 Yahoo CSV,如下所示。
我用来下载的代码来自 Yahoo 的 CSV
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+^N225+^GSPC+^GDAXI+^FCHI+^HSI+^IXIC+^FTSE&f=l1d14n");
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
inBuf = new Byte[10000000];
int bytesToRead = Convert.ToInt32(inBuf.ToString().Length);
int bytesRead=0;
while(bytesToRead>0)
{
int n = str.Read(inBuf,bytesRead,bytesToRead);
if(n==0)
{
break;
}
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream("C:\\VSS Working Folder\\20th Jan 11 NewHive\\NewHive\\CSV\\new.csv", FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(inBuf,0,bytesRead);
str.Close();
fstr.Close();
return "CSV Downloaded Successfully!";
可能出了什么问题?
In my website I am trying to download a CSV which comes from Yahoo. It contains some data.
I am using the code given below to download CSV.
Problem:
I want to download and fetch all the data from Yahoo's CSV but the whole CSV is not getting created on my side.
Only some portion of the data is copied. So CSV is not downloaded with all its data.
I tried increasing the Buffer size but that didn't help
Data in Yahoo's CSV is as shown in below screenshot. This is the data I want to download
Data that I get in created CSV, when I download the same Yahoo's CSV is as shown below.
Code I am using to download the CSV from Yahoo
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+^N225+^GSPC+^GDAXI+^FCHI+^HSI+^IXIC+^FTSE&f=l1d14n");
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
inBuf = new Byte[10000000];
int bytesToRead = Convert.ToInt32(inBuf.ToString().Length);
int bytesRead=0;
while(bytesToRead>0)
{
int n = str.Read(inBuf,bytesRead,bytesToRead);
if(n==0)
{
break;
}
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream("C:\\VSS Working Folder\\20th Jan 11 NewHive\\NewHive\\CSV\\new.csv", FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(inBuf,0,bytesRead);
str.Close();
fstr.Close();
return "CSV Downloaded Successfully!";
What could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
inBuf.ToString()
为您提供“System.Byte[]”,该字符串的长度为 13。因此您只保存了 13 个字符,这只会为您提供10274.52,"1/ 2
从下载的文件中,您可以使用
inBuf.Length
获取长度。请注意,Length 返回一个 int,因此您无需转换为 int。inBuf.ToString()
gives you "System.Byte[]" and the length of that string is 13. So you are only saving 13 characters which will only give you10274.52,"1/2
from the downloaded file.You can get the length using
inBuf.Length
. Note that Length returns an int so you don't need to cast to an int.