Guys,
This may be a bit of a long shot, but my code below is working fine for moving one file to an ftp location, what I would ideally like to do is upload all the files in the folder '@c:\test' one by one and rename them according to the value on label1,text
So in theory I don't want to throw all files up in one lump but rather loop through each file one by one and rename with each file according to the value in the label1,text
will this have to be done on a new ftp request for each file? can the below be adapted?
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpserver" + "label1.text");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
Image img = Image.FromFile(@"C:\Winter.jpg"); // notice single back-slash
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] fileContents = ms.ToArray();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
mike DelvottiPosted Jul 28, 2014, 9:13 AM
mike DelvottiPosted Jul 28, 2014, 7:18 AM
I have managed to get a little further with this, on my code example below I'm trying to log into ftp server and upload each file from the directory renaming them with a value from a textbox, I don't know if anyone can help me on a little problem though?
On my code below I get the following error: "The remote server returned an error: (530) Not logged in."
If I change the following line to: string ftpFile = FtpServer I then get the following error:
"The requested URI is invalid for this FTP command."
Almost seems to cure the URI problem it then won't even log in?
Code example:
string path = @"c:\test\";
string[] files = Directory.GetFiles(path, "*.jpg");
string FtpServer = "ftp://ftp1.server.co.uk";
string FtpUser = "User";
string FtpPassword = "Pass";
if (files != null)
{
//try
//{
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
string fileName = fi.Name;
string fileurl = path + @"/" + fileName;
string ftpFile = FtpServer + @"/" + fileName;
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);
myRequest.Credentials = new NetworkCredential(FtpUser, FtpPassword);
myRequest.Method = WebRequestMethods.Ftp.UploadFile;
myRequest.Timeout = 1000000;
myRequest.UseBinary = true;
myRequest.KeepAlive = true;
myRequest.ContentLength = fi.Length;
Image img = Image.FromFile(file);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] fileContents = ms.ToArray();
myRequest.ContentLength = fileContents.Length;
Stream requestStream = myRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
uploadResponse.Close();
}