In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to upload files on FTP in C#". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article on "how to upload files on FTP in C#" can help you solve your doubts.
1. By uploading files with FTP, first of all, to establish a FTP connection, generally establish a FTP connection, you need to know the relevant information about the FTP configuration. Generally, it is necessary to set up a ServiceFileInfo.cs file in Bean to record, which generally requires a FTP address, login user name and login password. Then access and read through other pages. The code style is as follows:
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 ";}
two。 Through the main method to read the information of the ServiceFileInfo.cs file under the Bean file, to establish a FTP connection. You also need to know clearly the path (Path) and file name (FileName) where you uploaded the file. According to this information, the main method calls another ftpOperation.cs file written in Bean (this .cs file mainly writes some operation methods about FTP) to perform FTP access operations.
The main method calls the FTP operation code
ExecutionResult exeRes = this.ftpOperation.UploadFile (textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt"); / / .txt is the suffix name of the file
The method of ftpOperation.cs file about FTP operation in Bean file
Public ExecutionResult UploadFile (string vIMSPath, string vUID, string vPassword, string vLocalPath) {ExecutionResult result = new ExecutionResult (); result = connectState (vIMSPath, vUID, vPassword, vLocalPath); / / call the following code method if (result.Status) {File.Delete (vLocalPath);} return result;}
ConnectState () method
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 () method
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 ("illegal file or directory name!");} bool flag2 = File.Exists (LocalFullPath); if (! flag2) {throw new Exception ("Local file does not exist!") } 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 ("invalid file or directory name!");} 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 ("illegal file name!") ;} bool flag2 =! OverWriteRemoteFile & & this.FileExist (RemoteFileName); if (flag2) {throw new Exception ("the file with the same name already exists on the FTP service!") ;} 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 } after reading this, the article "how to upload files on FTP in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.