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

时间:2023-05-06
本文介绍了在c#中将多个文件上传到FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用以下方法将文件从本地服务器上传到 FTP 服务器,这里我正在创建一个新连接并启动一个新会话,每个文件上传和关闭都相同.如何在 C# 的单个启动会话中实现这一点.

i'm using the below method to upload files from local server to FTP server, here i'm creating a new connection and initiating a new session each and every file uploading and closing the same. how to achieve this in single initiated session in c#.

这是我的代码

public bool UploadTempFilesToFTP()
    {
        string[] fileList;
        try
        {
            ConfiguredValues conObj = new ConfiguredValues();
            conObj.PickTheValuesFromConfigFile();
            fileList = Directory.GetFiles(conObj.tempPath);
            foreach (string FileName in fileList)
            {
                FtpWebRequest upldrequest = (FtpWebRequest)FtpWebRequest.Create(conObj.tempOutboundURL + FileName);


                upldrequest.UseBinary = true;
                upldrequest.KeepAlive = false;
                upldrequest.Timeout = -1;
                upldrequest.UsePassive = true;

                upldrequest.Credentials = new NetworkCredential(conObj.user, conObj.pass);

                upldrequest.Method = WebRequestMethods.Ftp.UploadFile;

                string destinationAddress = conObj.tempPath;

                FileStream fs = File.OpenRead(destinationAddress + FileName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
                Stream requestStr = upldrequest.GetRequestStream();
                requestStr.Write(buffer, 0, buffer.Length);
                requestStr.Close();
                requestStr.Flush();
                FtpWebResponse response = (FtpWebResponse)upldrequest.GetResponse();
                response.Close();
                File.Delete(destinationAddress + FileName);
            }
            Console.WriteLine("Uploaded Successfully to Temp folder");
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Upload failed. {0}", ex.Message);
            return false;
        }

    } 

推荐答案

我回答了一个老问题很奇怪,但我几乎尝试了所有方法将多个文件上传到 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);
                }
                
            }
        }
    }

这篇关于在c#中将多个文件上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:FtpWebRequest ListDirectory 不返回隐藏文件 下一篇:从 FTP 服务器下载名称包含特定字符串的文件

相关文章

最新文章