Exemplo n.º 1
0
 public bool Download(string filePath, string fileName, bool overWriteLocalFile, TransmissionProcess transProcess, out string errorinfo)
 {
     if (transProcess == null)
     {
         transProcess = new TransmissionProcess();
     }
     errorinfo = string.Empty;
     try
     {
         string str = Path.GetFileName(fileName);
         string path = filePath + @"\" + str;
         if (System.IO.File.Exists(path) && !overWriteLocalFile)
         {
             transProcess.TransStatus = TransferStatus.Failed;
             errorinfo = string.Format("本地文件{0}已存在,无法下载", path);
             return false;
         }
         if (System.IO.File.Exists(path))
         {
             try
             {
                 System.IO.File.Move(path, path);
             }
             catch
             {
                 transProcess.TransStatus = TransferStatus.Failed;
                 errorinfo = "本地文件被占用!无法覆盖!";
                 return false;
             }
         }
         string str3 = "ftp://" + this.FtpServer + "/" + fileName;
         this.Connect(str3);
         this.FtpRequest.Credentials = new NetworkCredential(this.FtpUserName, this.FtpPassword);
         FtpWebResponse response = (FtpWebResponse) this.FtpRequest.GetResponse();
         Stream responseStream = response.GetResponseStream();
         long contentLength = response.ContentLength;
         int count = 0x800;
         byte[] buffer = new byte[count];
         transProcess.FileName = str3;
         transProcess.TotalBytes = contentLength;
         if (transProcess.TransStatus != TransferStatus.InTransit)
         {
             transProcess.TransStatus = TransferStatus.InTransit;
         }
         int bytes = responseStream.Read(buffer, 0, count);
         transProcess.AppendBytes(bytes);
         FileStream stream2 = new FileStream(path, FileMode.Create, FileAccess.Write);
         while (bytes > 0)
         {
             stream2.Write(buffer, 0, bytes);
             bytes = responseStream.Read(buffer, 0, count);
             transProcess.AppendBytes(bytes);
         }
         responseStream.Close();
         stream2.Close();
         response.Close();
         transProcess.FinishBytes();
         return true;
     }
     catch (Exception exception)
     {
         transProcess.TransStatus = TransferStatus.Failed;
         errorinfo = string.Format("因{0},无法下载", exception.Message);
         return false;
     }
 }
Exemplo n.º 2
0
 public void Upload(string filename, string serverPath, TransmissionProcess transProcess)
 {
     if (transProcess == null)
     {
         transProcess = new TransmissionProcess();
     }
     FileInfo info = new FileInfo(filename);
     string str = string.Format("ftp://{0}/{1}/{2}", this.FtpServer, serverPath, info.Name);
     transProcess.FileName = str;
     transProcess.TotalBytes = info.Length;
     this.Connect(str);
     this.FtpRequest.KeepAlive = false;
     this.FtpRequest.Method = "STOR";
     this.FtpRequest.ContentLength = info.Length;
     int count = 2048;
     byte[] buffer = new byte[count];
     FileStream stream = info.OpenRead();
     try
     {
         Stream requestStream = this.FtpRequest.GetRequestStream();
         for (int i = stream.Read(buffer, 0, count); i != 0; i = stream.Read(buffer, 0, count))
         {
             if (transProcess.TransStatus != TransferStatus.InTransit)
             {
                 transProcess.TransStatus = TransferStatus.InTransit;
             }
             requestStream.Write(buffer, 0, i);
             transProcess.AppendBytes(i);
         }
         requestStream.Close();
         stream.Close();
         transProcess.TransStatus = TransferStatus.TranFinished;
     }
     catch (Exception exception)
     {
         transProcess.TransStatus = TransferStatus.Failed;
         throw new Exception(string.Format("向服务器上传文件时出错!\n\n{0}", exception.Message));
     }
 }