C# 实现FTP上传资料的示例

 更新时间:2020年12月07日 08:44:24   作者:農碼一生  
这篇文章主要介绍了C# 实现FTP上传资料的示例,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下

1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个ServiceFileInfo.cs文件进行记录,一般需要FTP地址、登录用户名和登录密码。然后通过其他页面进行访问读取。代码样式如下:

class ServiceFileInfo
  {
    // service1
    public static string txtFilePath = @"ftp://12.128.128.01/FileName/";
    //userid & password
    public static string txtUID = "username";
    public static string txtPWD = "password";
  }

2.通过主方法读取Bean文件下面的的ServiceFileInfo.cs文件的信息,去实现建立FTP连接。这里还需要清楚的知道你上传文件的路径(Path)和文件名称(FileName)。根据这些信息主方法去调用写着Bean中的另外一个ftpOperation.cs 文件(这个.cs文件中主要写一些关于FTP的操作方法),进行FTP访问操作。

  • 主方法调用FTP操作代码
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt为文件的后缀名
  •  Bean文件中ftpOperation.cs文件关于FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
    {
      ExecutionResult result = new ExecutionResult();
      result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//调用下面代码方法
      if (result.Status)
      {
        File.Delete(vLocalPath);
      }
      return result;
    }
  • connectState()方法
public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)
    {
      string operater = "";
      bool Flag = false;
      ExecutionResult result;
      result = new ExecutionResult();
      lock (lockObj)
      {
        try
        {
          operater = "Connet to FTP";
          FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);
          operater = "Upload file";
          Flag = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);
          if (Flag)
          {
            result.Status = true;
            result.Message = "Send to server OK";
          }
        }
        catch (Exception ex)
        {
          result.Status = false;
          result.Anything = "Mail";
          result.Message = operater + ":" + ex.Message;
        }
      }
      return result;
    }
  • UploadFile()方法
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));
        if (flag)
        {
          throw new Exception("非法文件名或目录名!");
        }
        bool flag2 = File.Exists(LocalFullPath);
        if (!flag2)
        {
          throw new Exception("本地文件不存在!");
        }
        FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);
        byte[] array = new byte[fileStream.Length];
        fileStream.Read(array, 0, (int)fileStream.Length);
        fileStream.Close();
        result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);
      }
      catch (Exception ex)
      {
        this.ErrorMsg = ex.ToString();
        throw ex;
      }
      return result;
    }



public bool UploadFile(byte[] FileBytes, string RemoteFileName)
    {
      bool flag = !this.IsValidFileChars(RemoteFileName);
      if (flag)
      {
        throw new Exception("非法文件名或目录名!");
      }
      return this.UploadFile(FileBytes, RemoteFileName, false);
    }


public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !this.IsValidFileChars(RemoteFileName);
        if (flag)
        {
          throw new Exception("非法文件名!");
        }
        bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
        if (flag2)
        {
          throw new Exception("FTP服务上面已经存在同名文件!");
        }
        this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");
        Stream requestStream = this.Request.GetRequestStream();
        MemoryStream memoryStream = new MemoryStream(FileBytes);
        byte[] array = new byte[1024];
        int num = 0;
        for (;;)
        {
          int num2 = memoryStream.Read(array, 0, array.Length);
          bool flag3 = num2 == 0;
          if (flag3)
          {
            break;
          }
          num += num2;
          requestStream.Write(array, 0, num2);
        }
        requestStream.Close();
        this.Response = (FtpWebResponse)this.Request.GetResponse();
        memoryStream.Close();
        memoryStream.Dispose();
        FileBytes = null;
        result = true;
      }
      catch (Exception ex)
      {
        this.ErrorMsg = ex.ToString();
        throw ex;
      }
      return result;
    }

以上就是C# 实现FTP上传资料的示例的详细内容,更多关于c# ftp上传的资料请关注脚本之家其它相关文章!

相关文章

  • C#中的位操作小结

    C#中的位操作小结

    在C#中位操作同C的位操作没有什么区别,位操作的速度相对较快,而且如果熟练的话,处理起来也相对方便,特别是在一些权限等相关的设置中
    2014-01-01
  • C#使用OleDb操作Excel和数据库的策略

    C#使用OleDb操作Excel和数据库的策略

    在C#编程中,使用OleDb可以方便地实现对Excel文件和数据库的操作,本文探讨了在C#中使用OleDb技术操作Excel和数据库的策略,文章详述了OleDb的定义、配置环境的步骤,并通过实际代码示例演示了如何高效读写Excel文件和交互数据库,需要的朋友可以参考下
    2024-05-05
  • C#多线程之线程池(ThreadPool)

    C#多线程之线程池(ThreadPool)

    这篇文章介绍了C#多线程之线程池(ThreadPool)的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#中自定义高精度Timer定时器的实例教程

    C#中自定义高精度Timer定时器的实例教程

    这篇文章主要介绍了C#中自定义高精度Timer定时器的实例教程,多线程的Timer编写需要注意线程安全的问题,需要的朋友可以参考下
    2016-04-04
  • c# 获取网页中指定的字符串信息的实例代码

    c# 获取网页中指定的字符串信息的实例代码

    c# 获取网页中指定的字符串信息的实例代码,需要的朋友可以参考一下
    2013-04-04
  • C#实现随机数产生类实例

    C#实现随机数产生类实例

    这篇文章主要介绍了C#实现随机数产生类,实例分析了C#随机数的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • C#实现回文检测的方法

    C#实现回文检测的方法

    这篇文章主要介绍了C#实现回文检测的方法,实例分析了C#使用栈进行回文检测的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C# 特性AttributeUsage简介与使用教程

    C# 特性AttributeUsage简介与使用教程

    这篇文章主要介绍了C# 特性AttributeUsage简介与使用教程,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • C#绘制时钟的方法

    C#绘制时钟的方法

    这篇文章主要为大家详细介绍了C#绘制时钟的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • c#读取XML多级子节点

    c#读取XML多级子节点

    本文主要介绍了c#读取XML多级子节点的方法。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03

最新评论