Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear friends

I am doing task regarding file uploading and asynchronously downloading.But am getting problem in file downloading.

File getting download in specified location but its bytes not getting transfer means 0 bytes files getting me.

my code is here

C#
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings["RemoteFileLocation"].ToString() + lblFolderName.Text);
                        ftpRequest.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["UsrNm"].ToString(), ConfigurationSettings.AppSettings["Pwd"].ToString());
                        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                        FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
                        StreamReader streamReader = new StreamReader(response.GetResponseStream());

                        string line = streamReader.ReadLine();
                        while (!string.IsNullOrEmpty(line))
                        {

                            using (WebClient webClient = new WebClient())
                            {
                                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                                webClient.DownloadFileAsync(new Uri(string.Concat(ConfigurationSettings.AppSettings["RemoteFileLocation"].ToString(), lblFolderName.Text, "/", line)), string.Concat(savePath, "/", line));
                                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                                btnDelete.Enabled = false;
                                btnDownload.Enabled = false;
                                button1.Enabled = false;
    
                            }
                            
                            line = streamReader.ReadLine();
                        }
                        streamReader.Close();


 private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {

            try
            {
                progressBar1.Value = e.ProgressPercentage;
                if (e.ProgressPercentage == 100)
                {
                    aa = aa + 1;
                    if (aa >= 1)
                    {
                        lblStatus.Text = (aa).ToString() + " Files Downloaded";
                        if (aa == count)
                        {
                            lblStatus.Text = "Downloading Complete";
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            

        }


please help me.
Posted

 
Share this answer
 
The WebClient's getting disposed at the bottom of the using loop. That probably kills the transfer. Try maintaining a single WebClient and downloading multiple files through it, or do it yourself with WebRequests.
 
Share this answer
 
Hey All finally got solution.

It solved by adding credential.

webClient.Credentials = new Net.NetworkCredential("username","pwd");


Thanks to all for reply
 
Share this answer
 
v2

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