当前位置:网站首页>Local file upload FTP or remote directory

Local file upload FTP or remote directory

2022-06-13 07:03:00 AndroidOrCSharp

1. Upload to FTP

 /// <summary>
        ///  Upload files FTP
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="workDate"></param>
        /// <param name="batchNo"></param>
        /// <returns></returns>
        public bool UploadFiles1(string localFile,string workDate,string batchNo)
        {
            string FTPServer = string.Empty;
            string FTPPort = string.Empty;
            string FTPUser = string.Empty;
            string FTPPwd = string.Empty;
            string SendDir = string.Empty;
            string dir = string.Empty;
            FileStream fs = null;
            Stream strm = null;
            try
            {
                GetFTPServerCfg("0004", ref FTPServer, ref FTPPort, ref FTPUser, ref FTPPwd,ref SendDir);
                FileInfo fileInf = new FileInfo(localFile);
                dir = "ftp://" + FTPServer + "/" + SendDir + "/"+ workDate + "_" + batchNo;
                string uri = "ftp://" + FTPServer + "/" + SendDir + "/" + workDate + "_" + batchNo +"/"+ fileInf.Name;
                MakeDir(dir, reqFTP, FTPUser, FTPPwd);
                Connect(uri, FTPUser, FTPPwd);    

                //  The default is true, The connection won't be closed 
                //  Executed after a command 

                reqFTP.KeepAlive = false;
                //  Specify what command to execute 

                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                //  Notify the server of the size of the file when uploading it 

                reqFTP.ContentLength = fileInf.Length;
                //  The buffer size is set to kb 
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];

                int contentLen;

                //  Open a file stream (System.IO.FileStream)  To read the uploaded file 
                fs = fileInf.OpenRead();


                //  Write the uploaded file to the stream 
                strm = reqFTP.GetRequestStream();

                //  Every time I read the file stream kb
                contentLen = fs.Read(buff, 0, buffLength);

                //  Stream content doesn't end 
                while (contentLen != 0)
                {
                    //  Take the content from file stream  write in upload stream 
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                return true;
            }
            catch (Exception ex)
            {
                //SystemMessge.Warning(ex.Message, " Upload error ");
                return false;
            }
            finally
            {
                //  Close both streams 
                if (strm != null)
                {
                    strm.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

  /// <summary>  
         ///  Create folder   
         /// </summary>   
           public void MakeDir(string uri, FtpWebRequest reqFTP, string ftpUserID, string ftpPassword,string fileName)
        {
            try
            {

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);// Chinese file name 
                // Determine whether the folder exists    Create... If it doesn't exist ( Only one level directory can be created )
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (line.Contains(fileName))
                    {
                        response.Close();
                        reader.Close();
                        return;
                    }

                    line = reader.ReadLine();
                }
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.UseBinary = true;
                response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                ftpStream.Close();
                reader.Close();
                response.Close();
            }
            catch (Exception ex)
            {
            }
        }

  #region  Connect to server 
        /// <summary>
        ///  Connect to server 
        /// </summary>
        /// <param name="path">FTP Address </param>
        /// <param name="ftpUserID"> user name </param>
        /// <param name="ftpPassword"> password </param>
        private void Connect(String path ,string ftpUserID,string ftpPassword)
        {
            //  according to uri establish FtpWebRequest object 
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

            //  Specify the data transfer type 
            reqFTP.UseBinary = true;

            // ftp User name and password 
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            reqFTP.KeepAlive = false;
        }
        #endregion

2. Upload to remote directory

   // Complete one TANO The corresponding instructions are uploaded to FTP
                Dir += "\\" + WorkDate + "_" + BatchNo;
                List<FileInfo> files = new List<FileInfo>();
                // Get all the files that need to be uploaded under the folder 
                files.AddRange(new DirectoryInfo(Dir).GetFiles());
                log.Info(string.Format(" Start to execute the function of uploading the local remittance instruction to the remote server "));
                GetFTPServerCfg("0005", ref FTPServer, ref FTPPort, ref FTPUser, ref FTPPwd, ref SendDir);
                SendDir = @"\\" + FTPServer + @"\" + SendDir + WorkDate + "_" + BatchNo;
                // Determine whether the remote folder exists , Create if it does not exist 
                if (!Directory.Exists(SendDir))
                {
                    Directory.CreateDirectory(SendDir);
                }
                foreach (FileInfo file in files) 
                {
                    UploadFiles(Dir+"\\"+file.Name,SendDir, file.Name);
                }            

        /// <summary>
        ///  Upload files FTP
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="SendDir"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public void UploadFiles(string localFile, string SendDir,string fileName)
        {
            //  establish WebClient example 
            File.Copy(localFile, SendDir + "/" + fileName, true);
        }

 

原网站

版权声明
本文为[AndroidOrCSharp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550514198.html