久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在c#中將多個文件上傳到FTP

Upload Multiple files to FTP in c#(在c#中將多個文件上傳到FTP)
本文介紹了在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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 成av在线| 国产精品久久久久久久久久免费看 | aaa一区 | 亚洲不卡在线视频 | 欧美综合一区二区三区 | 欧美成人一区二区三区片免费 | a级片在线观看 | 欧美精品一区二区三区四区 | 精品久久久久香蕉网 | 久久国产精品一区 | 福利网站在线观看 | 国产精品免费视频一区 | 免费黄色a视频 | 国产成人精品一区二 | av中文字幕在线观看 | 99在线播放 | 亚洲欧美中文日韩在线v日本 | 日韩在线不卡 | www国产成人免费观看视频,深夜成人网 | 亚洲国产成人在线 | 97高清国语自产拍 | 欧美一区二区 | 日本免费小视频 | 亚洲一区 | 免费av一区二区三区 | 欧美一区两区 | 国产免费一区二区三区 | 国产真实精品久久二三区 | 亚洲成人网在线播放 | 福利影院在线看 | 激情一区二区三区 | 日韩中文字幕av | 精品无码久久久久久国产 | 成人午夜在线视频 | 日本色综合 | 一区二区三区在线 | 久久国内精品 | av片在线免费看 | 免费在线播放黄色 | 午夜日韩| 国产精品国产a级 |