-1

I have a link that returns a CSV file. When I open it in a browser (Chrome, Firefox,...) the size of file that's downloaded is 86 KB, but when I want to download it with the code below, the size is just 25 KB and when I open the downloaded file it doesn't have correct data (means no columns and can't read data)

You can try it in browser and code

http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462

string url = "http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462";
 WebClient wc = new WebClient();
 wc.DownloadFile(url, "111.csv");
HasaniH
  • 8,232
  • 6
  • 41
  • 59
SajjadZare
  • 2,487
  • 4
  • 38
  • 68

3 Answers3

1

webClient is returning you zip file instead of plain text /csv file I changed wc output file extension to zip and it is working... zip will contain file that you specified in argument

screenshot from RestClient

0

As Akshay Sandhu pointed out the downloaded file is compressed with the gzip encoding and that is why it appears as corrupted when trying to open it as a csv. To download the file and automatically decode it please refer to these two SO answers. First download the file using the HttpWebRequest class instead of the WebClient class as done here:

How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)

Then make sure the file is automatically decompressed. Check this out

Automatically decompress gzip response via WebClient.DownloadData

Here is the working code:

string url = "http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462";
string path = "111.csv";

using (FileStream fileStream = new FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = WebRequestMethods.Http.Get;
    request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

    const int BUFFER_SIZE = 16 * 1024;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            var buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            do
            {
                bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
                fileStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
        }
    }
}
prenone
  • 302
  • 2
  • 20
0

You need to decompress the gzip, before you read it to file.

var url = new Uri("http://tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=43283802997035462");
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var fileName = "111.csv";

using (WebClient wc = new WebClient())
using (Stream s = File.Create(Path.Combine(path, fileName)))
using (GZipStream gs = new GZipStream(wc.OpenRead(url), CompressionMode.Decompress))
{
    //Saves to C:\Users\[YourUser]\Desktop\111.csv
    gs.CopyTo(s);
}
Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24