How to download a CSV file in website in ASP.NET
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 -=开发者_如何学C 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?
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 you 10274.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.
精彩评论