C#怎么实现FTP上传文件
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,本文小编为大家详细介绍"C#怎么实现FTP上传文件",内容详细,步骤清晰,细节处理妥当,希望这篇"C#怎么实现FTP上传文件"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1
千家信息网最后更新 2025年11月14日C#怎么实现FTP上传文件
本文小编为大家详细介绍"C#怎么实现FTP上传文件",内容详细,步骤清晰,细节处理妥当,希望这篇"C#怎么实现FTP上传文件"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
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#
代码
信息
文章
内容
目录
目录名
登录
妥当
清楚
后缀
地址
密码
思路
新知
更多
样式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器邮件安全
网警可以做网络安全工程师
信用卡持卡人数据库
数据库语言
服务器ip管理办法
安徽 网络安全专业
小米手环6软件开发
大时代陈顶天软件开发失败
物联网软件开发用什么需要
大唐互联网科技有限公司
网络安全法实施的重要意义
威动 服务器
软件开发经验风险及决策
r410服务器电源接口
内蒙古网络安全审查办法
微信小程序怎么配置数据库
串口服务器管理地址
软件开发资金来源渠道有哪些
web基础软件开发与设计试题
北京妙月互联网科技
lux查数据库空间大小
运用网络技术来盗窃
珠海银行招聘软件开发
国家网络安全形势分析
广东雄企产业互联网科技有限公司
服务器安全自主可控
服务器的raid
命令行改数据库ip地址
深圳市域浪网络技术...
环境监测数据库设计