Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this ftp upload, upload file from my local to ftp folder, but my problem is, I need to refresh the FTP Folder to view my uploaded file.

Pls help.

What I have tried:

using System.Net;
using System.IO;

//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpAdd + "/" + Path.GetFileName(filePath));

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;

//Load the file
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];

stream.Read(buffer, 0, buffer.Length);
stream.Close();

//Upload file
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
                    
request = null;
MessageBox.Show("Uploaded Successfully");
Posted
Updated 26-Mar-23 20:11pm
Comments
Dave Kreskowiak 12-Dec-18 0:36am    
You're going to have to describe what "refresh the FTP Folder" means. Does that mean you have to hit F5 in the Explorer window on the server to see the file? Does it mean you have to refresh the view on the client after you upload the file to show the current files on the remote FTP server? What?
berrymaria 12-Dec-18 0:49am    
Yes Sir, correct. I have to right click then click Refresh button in the explorer..
Dave Kreskowiak 12-Dec-18 8:30am    
You don't have any control over that in your code.
berrymaria 12-Dec-18 22:06pm    
Ohh i see, if that's the case.. then i'll rest my case.. thanks for your reply..

 
Share this answer
 
public static void Upload(string f)
{
string ftpserverIP = System.Configuration.ConfigurationManager.AppSettings["ftpserverIP"];
string username = System.Configuration.ConfigurationManager.AppSettings["username"];
string password = System.Configuration.ConfigurationManager.AppSettings["password"];
string portno = System.Configuration.ConfigurationManager.AppSettings["portno"];
string FromLocalFile = System.Configuration.ConfigurationManager.AppSettings["FromLocalFile"].ToString();
string ToServerFile = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ToServerFile"]).ToString();
string RemoteDir = System.Configuration.ConfigurationManager.AppSettings["RemoteDir"].ToString();
try
{
string uri = ftpserverIP + ToServerFile + "/";
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{

}
string file1 = string.Format(f);

string path1 = Path.Combine(RemoteDir, DateTime.Now.ToString("ddd MM.dd.yyyy"), Path.GetFileName(file1));
//call to CreateRemoteDir() method.
Program.CreateRemoteDir();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpserverIP + "/" + path1);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = true;
request.UseBinary = true;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(file1);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
catch (WebException wEx)
{
Console.WriteLine(wEx.Message, "Upload Error");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, "Upload Error");
}
}
 
Share this answer
 
Comments
Dave Kreskowiak 27-Mar-23 8:00am    
You didn't read the question at all. All you saw was "FTP" and thought it would be a good idea to just post your code. Your "answer" has nothing to do with the question that was asked.
berrymaria 9-Jul-23 20:35pm    
hahah yes he didn't read carefully. lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900