c#中从本地到ftp服务器的传输文件

时间:2015-10-14 11:31:16

标签: c# ftp

我想将文件从本地传输到ftp(文件传输协议)服务器,通常它适用于小文件,但如果我有一个大文件我有这个问题("连接到在页面加载")

时重置了服务器

这是我的代码:

            //FTP Server URL.
            string ftp = ipFTP;

            //FTP Folder name. Leave blank if you want to upload to root folder.
            string ftpFolder = dossier_DAO;

            byte[] fileBytes = null;

            //Read the FileName and convert it to Byte array.
            string fileName = Path.GetFileName(FileUpload1.FileName);
            using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
            {
                fileBytes = FileUpload1.FileBytes;
                fileStream.Close();
            }

            try
            {
                //Create FTP Request.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                //Enter FTP Server credentials.
                request.Credentials = new NetworkCredential(loginFTP, pwdFTP);
                request.ContentLength = fileBytes.Length;
                request.UsePassive = true;
                request.UseBinary = true;
                request.ServicePoint.ConnectionLimit = fileBytes.Length;
                request.EnableSsl = false;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(fileBytes, 0, fileBytes.Length);
                    requestStream.Close();
                }

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                //lblMessage.Text += fileName + " uploaded.<br />";


                response.Close();
            }
            catch (WebException ex)
            {
                throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
            }

1 个答案:

答案 0 :(得分:0)

我必须在网络配置中添加此代码:

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="900000000000" />
      </requestFiltering>
    </security>
 </system.webServer>