kaliliP kaliliP - 1 month ago 14
C# Question

transfere file from local to ftp server in c#

I want to transfere a file from local to ftp (File Transfer Protocol) server, normaly it works with a small file , but if i have a big size of file i have this probleme ("The connection to the server was reset while the page loads")

this is my code :

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

Answer Source

I must add this code in the web config :

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