2009-09-30 15 views
0

J'essaye d'envoyer des fichiers via FTP très souvent à des machines distantes. J'ai écrit avec succès tout le code pour l'envoi/réception. Maintenant, je veux envoyer des fichiers différents (ex: 3 fichiers) à différentes adresses IP (3 Ips). Comment faire?Téléchargement/Téléchargement de plusieurs fichiers Simultanément en utilisant FTPWebrequest Classe

public bool UploadFile(string file_to_upload,int attempts) 
    { 


      if (System.IO.File.Exists(file_to_upload)) 
      { 
        FtpWebResponse response = null; 
        FtpWebRequest ftprequest= null;   
        int Appendinglength = 0; 
        int TotalLength = 0; 
        pointer = 1; 
        long FTPFilesize = 0; 
        while (--attempts >= 0) 
        { 
         try 
         { 
          FileInfo finfo = new FileInfo(file_to_upload); 
          TotalLength = Convert.ToInt32(finfo.Length); 

          ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + finfo.Name)); 
          ftprequest.Credentials = new NetworkCredential(Uname, Pwd); 
          ftprequest.UseBinary = true; 
          ftprequest.Timeout = 500; 
          ftprequest.KeepAlive = false; 
          if (ISFtpFileExists(finfo.Name, out FTPFilesize)) 
          { 
           ftprequest.Method = WebRequestMethods.Ftp.UploadFile; 
          } 
          else 
          { 
           ftprequest.Method = WebRequestMethods.Ftp.UploadFile; 
          } 

          //ftprequest.ContentLength = finfo.Length - FTPFilesize; 
          ftprequest.UsePassive = false; 
          fstr = new FileStream(file_to_upload, FileMode.Open); 
          ToWriteStream = ftprequest.GetRequestStream(); 
          //response = (FtpWebResponse)ftprequest.GetResponse(); 
          attempts = 3000; 
          //fstr.Seek(FTPFilesize, SeekOrigin.Begin); 
          byte[] buffer = new byte[bufferLength]; 
          fstr.Read(buffer, 0, bufferLength); 
          startuptime = DateTime.Now; 
          Exoccur("Resuming Upload.."); 
          while (pointer != 0) 
          { 
           ToWriteStream.Write(buffer, 0, pointer); 
           Appendinglength += pointer; 
           int prg = CalculateProgress(Appendinglength, TotalLength); 
           PrgUpdate(prg); 
           //lblupdate(prg); 
           pointer = fstr.Read(buffer, 0, bufferLength); 
          } 
          ToWriteStream.Close(); 
          fstr.Close(); 
          break; 
         } 
         catch 
         { 
          //ToWriteStream.Close(); 
          fstr.Close(); 
          if (attempts == 2999) 
          { 
           PrgUpdate(0); 
          } 
          Appendinglength = 0; 
          ftprequest.Abort(); 
          ftprequest = null; 
          Thread.Sleep(500); 

         } 
         finally 
         { 
          //attempts = 3000; 
         } 
        } 
        enduptime = DateTime.Now; 
        TimeSpan timetaken = enduptime.Subtract(startuptime); 
        double d = timetaken.TotalMilliseconds; 
        d = d/1000; 
        Exoccur(Convert.ToString(d)); 
      } 
      else 
      { 
       Exoccur("File Doesnt occur"); 

      } 
      return true; 
    } 

celui ci-dessus est pour le téléchargement FTP

public bool DownloadFile(string DestPath, string file_to_download) 
    { 
     PrgUpdate(0); 
     FtpWebRequest ftprequest = null; 
     int TotalLength = 0; 
     WebResponse response = null; 
     FileInfo fin = null; 
     try 
     { 
      int Appendinglength = 0; 
      if (KeepLength != 0) 
      { 
       TotalLength = KeepLength; 
      } 
      else 
      { 
       //FileStream fst = new FileStream(DestPath, FileMode.Create); 
       TotalLength = Convert.ToInt32(GetFileSize_Remotemachine(file_to_download)); 
      } 
      fin = new FileInfo(DestPath); 
      //FtpWebRequest ftprequest; 
      ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + file_to_download)); 
      ftprequest.Credentials = new NetworkCredential(Uname, Pwd); 
      ftprequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      ftprequest.UseBinary = true; 
      ftprequest.KeepAlive = false; 
      if (fin.Exists) 
      { 
       ftprequest.ContentOffset = fin.Length; 
       fst = new FileStream(DestPath, FileMode.Append); 
      } 
      else 
      { 
       fst = new FileStream(DestPath, FileMode.Create); 
      } 
      response = (WebResponse)ftprequest.GetResponse(); 
      Stream GetStream = response.GetResponseStream(); 
      byte[] buffer = new byte[bufferLength]; 
      pointer = GetStream.Read(buffer, 0, bufferLength); 
      DateTime starttime = DateTime.Now; 
      while (pointer != 0) 
      { 
       fst.Write(buffer, 0, pointer); 
       Appendinglength += pointer; 
       PrgUpdate(CalculateProgress(Appendinglength, TotalLength)); 
       pointer = GetStream.Read(buffer, 0, bufferLength); 
      } 
      fin = new FileInfo(DestPath); 
      if ((long)TotalLength == fin.Length) 
      { 
       Exoccur("File download completed."); 
      } 
      else 
      { 
       PrgUpdate(100); 
      } 
      DateTime endtime = DateTime.Now; 
      TimeSpan diff = endtime.Subtract(starttime); 
      GetStream.Close(); 
      fst.Close(); 
      response.Close(); 
      double d = diff.TotalMilliseconds; 
      d = d/1000; 
      KeepLength = 0; 
      Exoccur(Convert.ToString(d)); 
     } 
     catch (Exception ex) 
     { 
      ftprequest.Abort(); 
      KeepLength = TotalLength; 
      PrgUpdate(0); 
      Exoccur(ex.Message); 
      return false; 
     } 
     finally 
     { 
      //response.Close(); 
      fst.Close(); 
     } 
     return true; 
    } 

et celui-ci est à télécharger

+1

Si vous avez déjà écrit du code, pourquoi ne le partagez-vous pas? –

+0

sûr que je l'afficherai – karthik

Répondre

-3

je l'ai fait travailler, nous pouvons faire différentes implémentations i threads utilisées pour faire plusieurs téléchargements concurremment.it fonctionne

Questions connexes