且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在c#中将多个文件上传到FTP

更新时间:2023-01-15 11:47:56

我回答了一个老问题很奇怪,但我几乎尝试了所有方法将多个文件上传到 ftp 没有运气,而解决方案非常简单有效,使用 LOOPING - foreach 为我解决了这个问题 我使用下面的函数通过一个简单的步骤上传文件..

it's weird that i answer an old question but i try almost everything to upload multiple files to ftp with no luck while the solution is very simple and effective, using LOOPING - foreach solved the issue for me i use the below function to Upload the files in one simple step..

public void Uploadbulkftpfiles(string[] list)
    {
        bool ife;// is folder exists
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder");
            request.Credentials = new NetworkCredential("Username", "Password");
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            ife = true;
        }
        catch (Exception)
        {

            ife = false;
        }

        /////////////////////////////////////////////begin of upload process
        
        if (ife)//the folder is already exists
        {
            foreach (var str in list)
            {
                try
                {
                    FtpWebRequest requestUP2 = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder" + str);
                    requestUP2.Credentials = new NetworkCredential("UserName", "Password");
                    requestUP2.Method = WebRequestMethods.Ftp.UploadFile;
                    requestUP2.KeepAlive = false;
                    requestUP2.UsePassive = true;
                    using (Stream fileStream = File.OpenRead("ftp://ftpsite.com/folder" + str))
                    using (Stream ftpStream = requestUP2.GetRequestStream())
                    {
                        fileStream.CopyTo(ftpStream);
                    }
                }
                catch (Exception ex1)
                {
                    
                    MessageBox.Show(ex1.Message);
                }
                
            }
        }
        else if (!ife)
        {
            //CREATE THE FOLDER
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp:ftpsite/folder");
                request.Credentials = new NetworkCredential("UserName", "Password");
                request.Method = WebRequestMethods.Ftp.MakeDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (Exception excr) { MessageBox.Show(excr.Message); }
            
            

            //UPLOAD THE FILES
            foreach (var str in list)
            {
                try
                {
                    FtpWebRequest requestUP2 = (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/folder" + str);
                    requestUP2.Credentials = new NetworkCredential("UserName", "Password");
                    requestUP2.Method = WebRequestMethods.Ftp.UploadFile;
                    requestUP2.KeepAlive = false;
                    requestUP2.UsePassive = true;
                    using (Stream fileStream = File.OpenRead("ftp://ftpsite.com/folder" + str))
                    using (Stream ftpStream = requestUP2.GetRequestStream())
                    {
                        fileStream.CopyTo(ftpStream);
                    }
                }
                catch (Exception ex1)
                {
                    
                    MessageBox.Show(ex1.Message);
                }
                
            }
        }
    }