如何在ASP.NET中下载CSV文件

时间:2021-05-28 15:19:43

In my website I am trying to download a CSV which comes from Yahoo. It contains some data.

在我的网站上,我试图下载一个来自雅虎的CSV。它包含了一些数据。

I am using the code given below to download CSV.

我正在使用下面给出的代码下载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.

我想从雅虎的CSV中下载并获取所有数据但整个CSV并没有在我这边创建。

Only some portion of the data is copied. So CSV is not downloaded with all its data.

只有部分数据被复制。所以CSV并没有下载所有数据。

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

雅虎的CSV数据如下图所示。这是我想下载的数据。

如何在ASP.NET中下载CSV文件

Data that I get in created CSV, when I download the same Yahoo's CSV is as shown below.

当我下载相同的Yahoo的CSV时,我在创建CSV时得到的数据如下所示。

如何在ASP.NET中下载CSV文件

Code I am using to download the CSV from Yahoo

我正在使用的代码从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!";

What could be wrong?

可能是错的呢?

1 个解决方案

#1


1  

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.

inBuf.ToString()给你”系统。字节[]"那个字符串的长度是13。所以你只保存了13个字符只会给你10274.52,"1/2从下载文件中。

You can get the length using inBuf.Length. Note that Length returns an int so you don't need to cast to an int.

你可以用inBuf.Length得到长度。注意,长度返回一个int类型,因此不需要强制转换为int类型。

#1


1  

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.

inBuf.ToString()给你”系统。字节[]"那个字符串的长度是13。所以你只保存了13个字符只会给你10274.52,"1/2从下载文件中。

You can get the length using inBuf.Length. Note that Length returns an int so you don't need to cast to an int.

你可以用inBuf.Length得到长度。注意,长度返回一个int类型,因此不需要强制转换为int类型。