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.

4

1 回答 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

于 2013-09-05T07:04:08.923 回答