0

I'm trying to create a small program to download files given a link.

The problem I have is the following... when I download files like *.htm , *.txt there is no problem, but when I try to get a bigger file, like *.zip, *.bmp my program only downloads between 2kb-7kb.

I tried also using localhost, because I thought maybe there are some security restrictions for externals queries in some websites but it was the same. [I know the way I'm organizing now the code in the main file is far away from being the correct, but like I said, this is just a test]

My code:

    static DateTime lastUpdate;
    static long lastBytes = 0;

    static void Main()
    {
        MyTask();

        Console.ReadKey();
    }

    async static Task MyTask()
    {
        var wc = new WebClient();

        wc.DownloadProgressChanged += (sender, args) =>
            {
                Console.WriteLine("{0} - {1} % complete", ProgressChanged(args.BytesReceived), args.ProgressPercentage);
            };

        Task.Delay(150000).ContinueWith(ant =>
            {
                wc.CancelAsync();
                Console.WriteLine("ABORTED!");
            });

        //http://windows.php.net/downloads/releases/php-5.5.3-nts-Win32-VC11-x86.zip
        //await wc.DownloadFileTaskAsync("http://localhost/", "w-brand.png");
        //await wc.DownloadFileTaskAsync("http://oreilly.com", "webpage.htm");
        await wc.DownloadFileTaskAsync("http://windows.php.net/downloads/releases/", "php-5.5.3-nts-Win32-VC11-x86.zip");
    }

    static long ProgressChanged(long bytes)
    {
        if (lastBytes == 0)
        {
            lastUpdate = DateTime.Now;
            lastBytes = bytes;
            return 0;
        }

        var now = DateTime.Now;
        var timeSpan = now - lastUpdate;
        var bytesChange = bytes - lastBytes;
        var bytesPerSecond = timeSpan.Seconds != 0 ? bytesChange / timeSpan.Seconds : 0;

        lastBytes = bytes;
        lastUpdate = now;

        return bytesPerSecond;
    }

Any help would be appreciated.

2
  • 1
    why do you do this: Task.Delay(150000).ContinueWith Commented Sep 5, 2013 at 6:56
  • It's only because I want to limit the time I can be downloading a file, but the code with the files I am testing never goes into ... Commented Sep 5, 2013 at 7:04

1 Answer 1

1

Your code always downloads the index web-page.

The first parameter of DownloadFileTaskAsync is the web url, the second the local path where to store the file.

wc.DownloadFileTaskAsync("http://windows.php.net/downloads/releases/php-5.5.3-nts-Win32-VC11-x86.zip", @"c:\php-5.5.3-nts-Win32-VC11-x86.zip");

works for me

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thank you... It was easy ...I misread the parameters list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.