I'm creating a FTP client that will allow me upload files to a determined server picked from a combobox. My code works for a single file but trying to adapt it for more than one file has been challenging. I've read about asynchronous uploads but I can't get it through my head so any help will be greatly appreciated.
private void upload(string filepath, string address, string username, string password)
{
//CONNECT
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address + "/" + Path.GetFileName(filepath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
MessageBox.Show(response.WelcomeMessage + " to " + comboBox1.Text + " SERVER");
//PREPARE FILE
FileStream stream = File.OpenRead(filepath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//UPLOAD FILE
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
MessageBox.Show("Upload complete on " + comboBox1.Text + " SERVER " + response.StatusDescription);
}
I put the files I want to upload into a ListBox, this is the code:
private void button2_Click(object sender, EventArgs e)
{
//THIS WAS FOR SINGLE FILE UPLOAD
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
//textBox1.Text = openFileDialog1.FileName;
//MULTIPLE FILE UPLOAD
OpenFileDialog openFiles = new OpenFileDialog();
openFiles.Filter = "PDF, XML, TXT Files(*.pdf;*.xml;*.txt)|*.pdf;*.xml;*.txt|All Files(*.*)|*.*";
openFiles.Multiselect = true;
//openFiles.InitialDirectory = "C:\\";
//openFiles.RestoreDirectory = true;
openFiles.Title = "Select Files";
if (openFiles.ShowDialog() == DialogResult.OK)
{
foreach (string file in openFiles.FileNames)
{
listBox1.Items.Add(System.IO.Path.GetFileName(file));
}
}
}
Now, whenever I click "upload" I can connect to the server but It won't transfer the files the error is very straight forward:
Could not find file 'C:\Users\me\documents\visual studio 2010\Projects\projectname\project\bin\Debug\openFileDialog1'.
But I can't get through this part. How can I reference the filepath I want from the ListBox? This is my "upload_click" code, It's just one of the 3 servers I can access, but once i get the first one working, the other ones should be easy.
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "server" && listBox1.Items.Count > 0)
{
while (listBox1.Items.Count > 0)
{
upload(openFileDialog1.FileName, "ftp://000.000.0.000", "username", "password");
listBox1.Items.RemoveAt(0);
}
textBox1.Text = " ";
comboBox1.Text = "Select Server...";
pictureBox1.Image = null;
//listBox1.Items.Clear();
}
I feel like its just a simple thing to achieve, but I can't see it.
What am I doing wrong?
When you populate the listbox, you are stripping away the path and just including the filename.
When you go to upload, you are using just the filename and the path is missing so the program looks in the default directory.
To verify, leave the full path of the filename in the listbox and see if it works.
To fix, you'll have to save the full paths somewhere if you don't want them displayed in the listbox.