Csharp/C#教程:C# 实现FTP上传资料的示例分享

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

classServiceFileInfo { //service1 publicstaticstringtxtFilePath=@"ftp://12.128.128.01/FileName/"; //userid&password publicstaticstringtxtUID="username"; publicstaticstringtxtPWD="password"; }

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

主方法调用FTP操作代码 ExecutionResultexeRes=this.ftpOperation.UploadFile(textFilePath,txtUID,txtPWD,Path+"/"+FileName+".txt");//.txt为文件的后缀名  Bean文件中ftpOperation.cs文件关于FTP操作的方法 publicExecutionResultUploadFile(stringvIMSPath,stringvUID,stringvPassword,stringvLocalPath) { ExecutionResultresult=newExecutionResult(); result=connectState(vIMSPath,vUID,vPassword,vLocalPath);//调用下面代码方法 if(result.Status) { File.Delete(vLocalPath); } returnresult; } connectState()方法 publicstaticExecutionResultconnectState(stringvIMSPath,stringvUID,stringvPassword,stringfileName) { stringoperater=""; boolFlag=false; ExecutionResultresult; result=newExecutionResult(); lock(lockObj) { try { operater="ConnettoFTP"; FTPOperationftp=newFTPOperation(newUri(vIMSPath),vUID,vPassword); operater="Uploadfile"; Flag=ftp.UploadFile(fileName,Path.GetFileName(fileName),true); if(Flag) { result.Status=true; result.Message="SendtoserverOK"; } } catch(Exceptionex) { result.Status=false; result.Anything="Mail"; result.Message=operater+":"+ex.Message; } } returnresult; } UploadFile()方法 publicboolUploadFile(stringLocalFullPath,stringRemoteFileName,boolOverWriteRemoteFile) { boolresult; try { boolflag=!this.IsValidFileChars(RemoteFileName)||!this.IsValidFileChars(Path.GetFileName(LocalFullPath))||!this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath)); if(flag) { thrownewException("非法文件名或目录名!"); } boolflag2=File.Exists(LocalFullPath); if(!flag2) { thrownewException("本地文件不存在!"); } FileStreamfileStream=newFileStream(LocalFullPath,FileMode.Open,FileAccess.Read); byte[]array=newbyte[fileStream.Length]; fileStream.Read(array,0,(int)fileStream.Length); fileStream.Close(); result=this.UploadFile(array,RemoteFileName,OverWriteRemoteFile); } catch(Exceptionex) { this.ErrorMsg=ex.ToString(); throwex; } returnresult; } publicboolUploadFile(byte[]FileBytes,stringRemoteFileName) { boolflag=!this.IsValidFileChars(RemoteFileName); if(flag) { thrownewException("非法文件名或目录名!"); } returnthis.UploadFile(FileBytes,RemoteFileName,false); } publicboolUploadFile(byte[]FileBytes,stringRemoteFileName,boolOverWriteRemoteFile) { boolresult; try { boolflag=!this.IsValidFileChars(RemoteFileName); if(flag) { thrownewException("非法文件名!"); } boolflag2=!OverWriteRemoteFile&&this.FileExist(RemoteFileName); if(flag2) { thrownewException("FTP服务上面已经存在同名文件!"); } this.Response=this.Open(newUri(this.Uri.ToString()+RemoteFileName),"STOR"); StreamrequestStream=this.Request.GetRequestStream(); MemoryStreammemoryStream=newMemoryStream(FileBytes); byte[]array=newbyte[1024]; intnum=0; for(;;) { intnum2=memoryStream.Read(array,0,array.Length); boolflag3=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(Exceptionex) { this.ErrorMsg=ex.ToString(); throwex; } returnresult; }

上述就是C#学习教程:C# 实现FTP上传资料的示例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/903617.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐