narasappa J

narasappa J

  • NA
  • 147
  • 3.1k

show progress bar while downloading pdf from FTP on web appl

Nov 17 2017 5:09 AM
I am trying to download files from FTP. The requirement is to show a progress bar with Downloadingsize/Filesize and percentage.
I am able to display progress bar on Windows Form Application but On web application hosted on Local IIS server, I am getting a problem. I am using below code to download files from FTP on generic handler
 
  1. public bool DownLoadFilesFromFTp(string fileName,string ftpFolder)  
  2.        {  
  3.            //Create FTP Request.  
  4.            try  
  5.            {  
  6.                string Ftp_Host = System.Configuration.ConfigurationManager.AppSettings["Ftp_Host"];  
  7.                string Ftp_UserName = System.Configuration.ConfigurationManager.AppSettings["Ftp_UserName"];  
  8.                string Password = System.Configuration.ConfigurationManager.AppSettings["Password"];  
  9.                string downloadpath= System.Configuration.ConfigurationManager.AppSettings["downloadpath"];  
  10.                //Fetch the Response and read it into a MemoryStream object.  
  11.                string ftpurl = Ftp_Host + ftpFolder + "/" + fileName;  
  12.                FtpWebRequest reqFTP;  
  13.                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpurl));  
  14.                reqFTP.Credentials = new NetworkCredential(Ftp_UserName, Password);  
  15.                reqFTP.KeepAlive = false;  
  16.                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
  17.                reqFTP.UseBinary = true;  
  18.                reqFTP.Proxy = null;  
  19.                reqFTP.UsePassive = false;  
  20.   
  21.                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  22.                Stream responseStream = response.GetResponseStream();  
  23.                FileStream writeStream = null;  
  24.                
  25.                writeStream = new FileStream(downloadpath + fileName, FileMode.Create);                 int Length = 2048;  // 2048;  
  26.   
  27.                Byte[] buffer = new Byte[Length];  
  28.                int bytesRead = responseStream.Read(buffer, 0, Length);  
  29.                while (bytesRead > 0)  
  30.                {  
  31.                    writeStream.Write(buffer, 0, bytesRead);  
  32.                    bytesRead = responseStream.Read(buffer, 0, Length);  
  33.                }  
  34.   
  35.   
  36.                responseStream.Close();  
  37.                writeStream.Close();  
  38.                response.Close();  
  39.                return true;  
  40.            }  
  41.            catch (WebException wEx)  
  42.            {  
  43.                return false;  
  44.   
  45.            }  
  46.            catch (Exception ex)  
  47.            {  
  48.                return false;  
  49.   
  50.            }  
  51.        }  
  52.        public void ProcessRequest(HttpContext context)  
  53.        {  
  54.            DownLoadFilesFromFTp("FileName.pdf""Foldername");  
  55.             
  56.        }  
 
  1. class="progress CustomProgress">  
  2.         "FileProgress" class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">  
  3.               
  4.         
  
  •     
  •   
  •     "text/javascript">  
  •         $("#MainContent_btnDownload").click(function () {  
  •             $.ajax({  
  •                 type: "POST",  
  •                 url: "Downloader.ashx",  
  •                   
  •                 success: function (data) {  
  •                     
  •                     //alert(data);                  
  •                 }  
  •             });  
  •         });  
  •  I want to add below progress listener on my above ajax call 
     
    1. function progressHandlingFunction(e) {  
    2.     var TotalFileSize = GetFileSize( e.total);  
    3.     if (e.lengthComputable) {  
    4.         
    5.         
    6.         $("#FileProgress").show();  
    7.         var percentComplete = Math.round(e.loaded * 100 / e.total);  
    8.        
    9.         $("#FileProgress").css("width", percentComplete + '%').attr('aria-valuenow', percentComplete);  
    10.         $('#FileProgress span').text(percentComplete + "%");  
    11.          
    12.         $("#progressreport").html("Uploading  " + GetFileSize(e.loaded) + " of " + TotalFileSize);  
    13.           
    14.     }  
    15.     else {  
    16.         $('#FileProgress span').text('unable to compute');  
    17.     }  
    18. }  
     
    How can call this function on ajax request 

    Answers (1)