上传多个文件到FTP在C# [英] Upload Multiple files to FTP in c#

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

问题描述

我使用下面的方法从本地服务器的文件上传到FTP服务器,在这里我创建一个新的连接和启动每一个新的会话,每个文件上传和关闭相同。如何在C#中单发起的会话实现这一目标。



这是我的代码



 公共BOOL UploadTempFilesToFTP()
{
的String []的fileList;

{
ConfiguredValues conObj =新ConfiguredValues();
conObj.PickTheValuesFromConfigFile();
的fileList = Directory.GetFiles(conObj.tempPath);
的foreach(在字符串的fileList文件名)
{
的FtpWebRequest upldrequest =(的FtpWebRequest)FtpWebRequest.Create(conObj.tempOutboundURL +文件名);


upldrequest.UseBinary = TRUE;
upldrequest.KeepAlive = FALSE;
upldrequest.Timeout = -1;
upldrequest.UsePassive = TRUE;

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

upldrequest.Method = WebRequestMethods.Ftp.UploadFile;

串destinationAddress = conObj.tempPath;

的FileStream FS = File.OpenRead(destinationAddress +文件名);
字节[]缓冲区=新的字节[fs.Length]
fs.Read(缓冲液,0,buffer.Length);
fs.Close();
流requestStr = upldrequest.GetRequestStream();
requestStr.Write(缓冲液,0,buffer.Length);
requestStr.Close();
requestStr.Flush();
FtpWebResponse响应=(FtpWebResponse)upldrequest.GetResponse();
response.Close();
File.Delete(destinationAddress +文件名);
}
Console.WriteLine(成功上传到Temp文件夹);
返回真;
}
赶上(异常前)
{
Console.WriteLine(上传失败{0},ex.Message);
返回FALSE;
}

}


解决方案

FTP协议的目的是要求基础上的作品。



您开始使用的方法(在你的情况UploadFile)的请求。



你能做的唯一的事情就是将keepalive您的要求,以避免连接结束

  upldrequest.KeepAlive = TRUE; 



在每次请求你除了最后一个创造。这将使登录只有第一个的FtpWebRequest



然后,当您创建的最后的FtpWebRequest ,设置

  upldrequest.KeepAlive = FALSE; 

和它将关闭完成后连接。


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#.

this is my code

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

    } 

解决方案

The ftp protocol is intended to works on request basis.

You start a request with a method (in your case UploadFile).

The only thing you can do is to KeepAlive your request to avoid connection closing

upldrequest.KeepAlive = true;

on every request you create except the last one. This will make a login only the first FTPWebRequest.

Then when you create the last FTPWebRequest, set

upldrequest.KeepAlive = false;

and it will close the connection when done.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆