FTP服务器生成“错误:150打开文件下载的数据通道”

时间:2019-09-09 10:52:43

标签: c# asp.net ftp ftp-client ftpwebrequest

我已经编写了用于将文件从FTP服务器传输到本地服务器的代码,并且在传输2或3个文件后,它引发异常错误 远程服务器返回了错误:150打开用于下载文件的数据通道

所有文件已在FileZila中成功传输,但在asp.net应用程序中出现错误。

我执行以下代码

protected string TransferFile(string strPath, string strFtpPath, string strFtpUser, string strFtpPwd, string strSubClientFTPPath, string strSubClientFTPUser, string strSubClientFTPPwd, string strFileName, string strConvFileName, string strFileSize)
    {
        string strSuccess = "";
        FtpWebRequest reqFTP;
        try
        {

            Double Size = 0;
            string strLocalFileSize = "";
            if (File.Exists(strPath + strConvFileName))
            {
                Size = ((Math.Round(Convert.ToDouble(new FileInfo(strPath + strConvFileName).Length)) > 0 && Math.Round(Convert.ToDouble(new FileInfo(strPath + strConvFileName).Length)) < 1024) ? 1 : (Math.Round(Convert.ToDouble(new FileInfo(strPath + strConvFileName).Length) / 1024 + 0.0001)));
                strLocalFileSize = Size + "KB";
            }
            if (!File.Exists(strPath + strConvFileName) || strFileSize != strLocalFileSize)
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpPath + strFileName.Replace("#", "%23")));
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                reqFTP.EnableSsl = true;
                reqFTP.Timeout = Timeout.Infinite;
                reqFTP.KeepAlive = true;
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                //reqFTP.UseBinary = true;
                //reqFTP.UsePassive = false;
                reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPwd);
                //   lblTransferredFile.Text = "Transferring " + strFileName + "  ......";
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                //  lblTransferredFile.Text = "Transferring " + strFileName + "  ......";
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                //if (readCount > 0)
                //{
                FileStream outputStream = new FileStream(strPath +
                                                         strConvFileName, FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
                strSuccess = "";

            }
            else
            {
                strSuccess = "File Exists";
            }
        }
        catch (Exception ex)
        {
            strSuccess = ex.Message;
            ClientScript.RegisterStartupScript(this.GetType(), "strScript", "<script>alert('" + ex.Message.Trim().Replace("'", "") + "');</script>");
        }

        return strSuccess;
    }

1 个答案:

答案 0 :(得分:0)

我今天在安装kb 4519976之后遇到了这个问题。卸载此KB解决了问题。您能检查一下这是否也是您的解决方案。

上下文是一个ftps tls 1.2 ECHDE连接,该连接在启动额外连接的阶段失败。