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

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

Upload Multiple files to FTP in c#(在c#中將多個文件上傳到FTP)
本文介紹了在c#中將多個文件上傳到FTP的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用以下方法將文件從本地服務器上傳到 FTP 服務器,這里我正在創(chuàng)建一個新連接并啟動一個新會話,每個文件上傳和關閉都相同.如何在 C# 的單個啟動會話中實現(xiàn)這一點.

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;
        }

    } 

推薦答案

我回答了一個老問題很奇怪,但我?guī)缀鯂L試了所有方法將多個文件上傳到 ftp 沒有運氣,而解決方案非常簡單有效,使用 LOOPING - foreach 為我解決了這個問題 我使用下面的函數(shù)通過一個簡單的步驟上傳文件..

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模板網(wǎng)!

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

相關文檔推薦

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屋-程序員軟件開發(fā)技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 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 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 欧美精品在线看 | a毛片视频网站 | 欧美一区二区三区视频 | 欧美一级久久精品 | 亚洲一区二区在线免费观看 | 天天草视频| 精品久久99 | 国产9999精品 | 黄色网址大全在线观看 | 伊人天堂网 | 欧美人妖网站 | 亚洲精品欧美 | 在线精品一区二区三区 | 色综合天天综合网国产成人网 | 伊人网91| 久久av网 | 欧美日韩视频 | 欧美日韩专区 | 久草成人| 久久大陆| 欧美一区二区精品 | 亚洲+变态+欧美+另类+精品 | 91视频中文 | 97国产精品视频 | 亚洲视频1区 | 亚洲成人久久久 | 在线欧美一区二区 | 日本视频免费观看 | 在线一区视频 | 欧美日韩视频在线播放 | 欧美一级电影免费 | 亚洲日日夜夜 | 久久国产精品99久久久久久丝袜 | 国产一级免费视频 | 精品av天堂毛片久久久借种 | 国产亚洲精品a | 亚洲精品久久久久久久不卡四虎 | 成人在线精品视频 | 日韩精品免费在线观看 | 精品欧美一区二区在线观看视频 | 欧美性久久 |