服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 实现FTP上传资料的示例

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

2022-10-20 12:54農碼一生 C#

这篇文章主要介绍了C# 实现FTP上传资料的示例,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下

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

?
1
2
3
4
5
6
7
8
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操作代码
?
1
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt为文件的后缀名
  •  Bean文件中ftpOperation.cs文件关于FTP操作的方法
?
1
2
3
4
5
6
7
8
9
10
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()方法
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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()方法
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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上传的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/wml-it/p/12881936.html

延伸 · 阅读

精彩推荐
  • C#带你复习c# 托管和非托管资源

    带你复习c# 托管和非托管资源

    这篇文章主要介绍了c# 托管和非托管资源的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    团队buff工具人5702022-09-26
  • C#Unity实现鼠标拖动3D物体

    Unity实现鼠标拖动3D物体

    这篇文章主要为大家详细介绍了Unity实现鼠标拖动3D物体,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    罗松_ls5542022-10-11
  • C#C#实现托盘程序并禁止多个应用实例运行的方法

    C#实现托盘程序并禁止多个应用实例运行的方法

    这篇文章主要介绍了C#实现托盘程序并禁止多个应用实例运行的方法,涉及C#中NotifyIcon控件的使用及设置标志位控制程序只运行一个的实现技巧,具有一定参考...

    Jimmy.Yang8372021-11-04
  • C#Unity Shader实现序列帧动画效果

    Unity Shader实现序列帧动画效果

    这篇文章主要为大家详细介绍了Unity Shader实现序列帧动画效果 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    起个名字真的好难啊4282022-03-11
  • C#C#验证身份证号码正确性的实例代码(收藏)

    C#验证身份证号码正确性的实例代码(收藏)

    这篇文章主要介绍了C#验证身份证号码正确性的实例代码,包括18位号码和15位号码的校验,需要的朋友可以参考下...

    编编橙3682022-01-17
  • C#C#调用C++dll方法步骤

    C#调用C++dll方法步骤

    在本篇文章中小编给读者们整理了关于C#调用C++dll方法和步骤,需要的朋友们跟着操作下。...

    C#教程网10532022-07-06
  • C#浅谈C#下winform和JS的互相调用和传参(webbrowser)

    浅谈C#下winform和JS的互相调用和传参(webbrowser)

    下面小编就为大家带来一篇浅谈C#下winform和JS的互相调用和传参(webbrowser)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来...

    C#教程网5522021-12-13
  • C#C# MVC 微信支付教程系列之扫码支付代码实例

    C# MVC 微信支付教程系列之扫码支付代码实例

    本篇文章主要介绍了C# MVC 微信支付教程系列之扫码支付,非常具有实用价值,需要的朋友可以参考下。...

    南宫萧尘8912021-12-15