Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am trying to download zip files from a webaddress. For that I am using webclient concept. When I download the file for the first time it works fine. But for the next time the file downloaded was with 0KB. Where I am going wrong please help ASAP.

For your information, I hereby provided few lines for your reference.

VB
_WC = New System.Net.WebClient
            If System.IO.Directory.Exists(fDownloads) Then
                If System.IO.File.Exists(fDownloads & "\" & cellval) Then
                    System.IO.File.Delete(fDownloads & "\" & cellval)
                    _WC.DownloadFileAsync(New Uri(strWebAddress & "/itrol/" & cellval), fDownloads & "\" & cellval, cellval)
                    System.Threading.Thread.Sleep(1500)
                Else
                    '_WC.DownloadFileAsync((strWebAddress & "/itrol/" & cellval), fDownloads & "\" & cellval, cellval)
                    _WC.DownloadFileAsync(New Uri(strWebAddress & "/itrol/" & cellval), fDownloads & "\" & cellval, cellval)
                    System.Threading.Thread.Sleep(1500)
                End If
            Else
                System.IO.Directory.CreateDirectory(fDownloads)
                _WC.DownloadFileAsync(New Uri(strWebAddress & "/itrol/" & cellval), fDownloads & "\" & cellval, cellval)
                System.Threading.Thread.Sleep(1500)
            End If


 _WC.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
                    _WC.CancelAsync()
                    _WC.Dispose()
                    _WC = Nothing
Posted
Updated 13-Feb-14 0:40am
v2

1 solution

If you start file download async, then you should handle DownloadFileCompleted event. async download means that you want to do the task in another thread, not to wait for it to complete. therefore you have to handle this event to progress on that job further when it finished. thread sleep doesn't guarantee that your download will finish in that sleep time, as in your case.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Test_WebClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                WebClient cl = new WebClient();
                // handle event
                cl.DownloadFileCompleted += cl_DownloadFileCompleted;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        static void cl_DownloadFileCompleted(object sender,
            System.ComponentModel.AsyncCompletedEventArgs e)
        {
            // do your job
        }
    }
}
 
Share this answer
 
v3
Comments
rahulaaditya 13-Feb-14 9:42am    
Hi Vedat, I am using 3.5 framework for my VS2008. I believe it is because of earlier fwk version it is not accepting threading.task namespace. Any option to overcome this?
Vedat Ozan Oner 13-Feb-14 10:45am    
you won't need it. just delete.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900