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

时间:2017-05-15 09:53:58

标签: 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 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;

    }

0 个答案:

没有答案