Search code examples
c#webclientdownloadfileasync

WebClient DownloadFileAsync() work Incorect


I use WebClient to download files from a server, but the downloaded file is not same (the file size is wrong):

Here's a screenshot (the downloaded file is on the right):

Downloaded file

And here's my code:

public void StartDownload(string fileToDownloadLink, string PathToSaveFile)         ///////// Try check file type..
    {
        try
        {
            using (webClient = new WebClient())
            {
                //webClient.Encoding = Encoding.UTF8;
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
                webClient.DownloadFileAsync(new Uri(fileToDownloadLink), PathToSaveFile);
            }
        }
        catch (WebException e)
        {
            MessageBox.Show(fileToDownloadLink);
            MessageBox.Show(e.ToString());
            Worker.CancelAsync();
            Application.Exit();
        }
    }

What's wrong?


Solution

  • I probably find why this happened.. Server support only ASCII encoding look on file size after upload to server: enter image description here

    I forced the coding and it worked :) Now files are same :)