I want to upload file from one server to another FTP server and following is my code to upload file but it is throwing an error as:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
This my code:
string CompleteDPath = "ftp URL";
string UName = "UserName";
string PWD = "Password";
WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
reqObj.Credentials = new NetworkCredential(UName, PWD);
FileStream streamObj = System.IO.File.OpenRead(Server.MapPath(FileName));
byte[] buffer = new byte[streamObj.Length + 1];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);
reqObj = null;
Can you please tell me where i am going wrong?
Please make sure your URL that you pass to
WebRequest.Create
has this format:There are easier ways to upload a file using .NET framework.
Easiest way
The most trivial way to upload a file to an FTP server using .NET framework is using
WebClient.UploadFile
method:Advanced options
If you need a greater control, that
WebClient
does not offer (like TLS/SSL encryption, ASCII mode, active mode, etc), useFtpWebRequest
, like you do. But you can make the code way simpler and more efficient by usingStream.CopyTo
:For even more options, including progress monitoring and uploading whole folder, see:
Upload file to FTP using C#
Here are sample code to upload file on FTP Server
Please make sure your ftp path is set as shown below.
The following script work great with me for uploading files and videos to another servier via ftp.
Here is the Solution !!!!!!
To Upload all the files from Local directory(ex:D:\Documents) to FTP url (ex: ftp:\{ip address}\{sub dir name})
You can also use the higher-level
WebClient
type to do FTP stuff with much cleaner code: