Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过单个FTP连接发送多个文件_C#_Visual Studio_Ftp - Fatal编程技术网

C# 通过单个FTP连接发送多个文件

C# 通过单个FTP连接发送多个文件,c#,visual-studio,ftp,C#,Visual Studio,Ftp,我正在尝试修改此FTP连接方法,以便在单个连接中上载多个文件 正如您所看到的,我有一个while循环,循环遍历文件名和文件路径数组,我将WebRequest设置为保持活动状态。我不确定我应该做些什么来阻止新的连接不断开放 这就是我得到的错误: The remote server returned an error: (550) File unavailable (e.g., file not found, no access). 提前谢谢 Public string FTPUploadMult

我正在尝试修改此FTP连接方法,以便在单个连接中上载多个文件

正如您所看到的,我有一个while循环,循环遍历文件名和文件路径数组,我将WebRequest设置为保持活动状态。我不确定我应该做些什么来阻止新的连接不断开放

这就是我得到的错误:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
提前谢谢

Public string FTPUploadMultipleFiles(string ftpURL, string Username, string Password, string[] filePaths, string[] fileNames)
    {
        string result = "OK";
        try
        {

            int Counter = 0;
            if (filePaths.Count() == fileNames.Count())
                while (Counter <= filePaths.Count() - 1)
                {
                    FileInfo fileInf = new FileInfo(filePaths[Counter]);
                    FtpWebRequest reqFTP;
                    // Create FtpWebRequest object from the Uri provided
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURL + "/" + fileNames[Counter]));
                    reqFTP.Credentials = new NetworkCredential(Username, Password);
                    reqFTP.KeepAlive = true;

                    // Specify the command to be executed.
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                    // Specify the data transfer type.
                    reqFTP.UsePassive = true;

                    reqFTP.UseBinary = true;
                    // Notify the server about the size of the uploaded file
                    reqFTP.ContentLength = fileInf.Length;
                    // The buffer size is set to 2kb
                    int buffLength = 204800;
                    byte[] buff = new byte[buffLength];
                    int contentLen;

                    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                    FileStream fs = fileInf.OpenRead();
                    try
                    {
                        // Stream to which the file to be upload is written
                        Stream strm = reqFTP.GetRequestStream();
                        // Read from the file stream 2kb at a time
                        contentLen = fs.Read(buff, 0, Convert.ToInt32(buffLength));
                        // Till Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);

                        }
                        // Close the file stream and the Request Stream
                        strm.Close();
                        fs.Close();
                        Counter++;

                    }



                    catch (Exception ex)
                    {
                        result = ex.Message;
                    }

                }
        }

        catch (Exception ex)
        {
            result = ex.Message;
        }

        return result;

    }
公共字符串FTPUploadMultipleFiles(字符串ftpURL、字符串用户名、字符串密码、字符串[]文件路径、字符串[]文件名)
{
字符串结果=“确定”;
尝试
{
int计数器=0;
if(filepath.Count()==fileNames.Count())

while(计数器)您的代码看起来不错。向我们展示一些证据,证明它关闭了连接。例如,我面临的问题是,在服务器端,它正在接收多个登录,但没有收到任何文件。我只需要它登录一次,并在一次登录期间传输所有文件……”远程服务器返回一个错误:(550)文件不可用(例如,找不到文件,无法访问)。“显示日志!+如何将“550文件不可用”与此问题联系起来?您真的是说上载单个文件可以工作,但在循环中会出错吗?是的,此代码在循环之外工作