0

I'm trying to write a code to download an XLSX (Excel 2007+) file by using WebClient in C#. The problem is, although the below (standard) code can download other files from the internet, it can't download this file, which is generated on the fly from the response of an ASPX page.

Here is my code:

public bool Download(string url, string targetFileName, out DownloadFinalState finalState)
        {
            finalState = DownloadFinalState.InitialState;
            try
            {
                Random rnd = new Random();
                string fname = Directory.GetCurrentDirectory() + "\\" + rnd.Next(10000, 99999) + targetFileName;
                WebClient Client = new WebClient();

                var ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                Client.Headers.Add(HttpRequestHeader.UserAgent, ua);
                Client.DownloadFile(url, fname);

                if (File.Exists(fname))
                {
                    finalState = DownloadFinalState.FileDownloadedSuccessfully;
                }
                else
                {
                    finalState = DownloadFinalState.NoExceptionButNoFile;
                }
                return true;
            }
            catch (Exception ex)
            {
                finalState = DownloadFinalState.ExceptionRaised;
                return false;
            }
        }

and Here is the file's URL (which is publicly available): http://www.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0

I also tried removing agent in headers and also tried HttpWebRequest -based method but none of them worked. In the case of above code, the file being downloaded in 4KB smaller than the actual file and is in a weird unreadable (for MS Excel) format.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Farshid
  • 5,134
  • 9
  • 59
  • 87
  • @Soner Gonul: Your edit made my question be disappeared from newest questions. – Farshid Nov 24 '13 at 13:47
  • It's more than clear that the problem is on the server generating this file. It's simply stripping some bytes from it. Hard to help without showing your server side code that is generating the file. Usually this is caused by not properly disposing some IDisposable resource. – Darin Dimitrov Nov 24 '13 at 14:07
  • @DarinDimitrov: I myself mentioned the case (which is generated on the fly from the response of an ASPX page) & this is not my server, this is a server I want to download a file from. – Farshid Nov 24 '13 at 14:25

1 Answers1

1

Yeah, that looks like an issue with the WebClient.DownloadFile method. Try the following, it works great for me:

class Program
{
    static void Main()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://www.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0");
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
        using (var response = request.GetResponse())
        using (var stream = response.GetResponseStream())
        using (var output = File.Create("test.xlsx"))
        {
            stream.CopyTo(output);
        }
    }
}

I am able to successfully open the downloaded file in Excel. In this example I have specified the decompression method on the request. It's also possible to be done with a WebClient but you will need to write a custom WebClient and set the property on the Request.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin. a few seconds ago I realized the problem is about gzip and I solved it. However your answer is truly correct and deserve being chosen as the correct answer. – Farshid Nov 24 '13 at 14:42
  • For those who might check here for their problem: use Fiddler to see if content is being gziped from the side of your web server and in case of problem being similar to mine, use the method told by @Darin or check here for an alternate solution: http://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient – Farshid Nov 24 '13 at 14:44