Answered ftp filename lost when uploading

  • Saturday, September 01, 2012 1:00 PM
     
      Has Code

    Im uploading a file but it keeps getting renamed. How would I prevent this from not happening but without using the the stream option. Here is my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    
    namespace ftpApp
    {
        public partial class Form1 : Form
        {
            string username;
            string password;
            string URLpath;
            string filePath;
            public Form1()
            {
                InitializeComponent();
                
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    username = textBox1.Text;
                    password = textBox2.Text;
                    URLpath = textBox3.Text;
                    filePath = textBox4.Text;
                    ConnectAndUpload();
                }
                catch (Exception error)
                {
                    MessageBox.Show("FTP UPLOAD FAILED, Please check all your details are correct",
           "FTP UPLOAD FAILURE",
           MessageBoxButtons.OK,
           MessageBoxIcon.Error);
                }
            }
            private void ConnectAndUpload()
            {
                WebClient Client = new WebClient();
                Client.Credentials = new System.Net.NetworkCredential(username, password);
                Client.BaseAddress = URLpath;
    
                Client.UploadFile(WebRequestMethods.Ftp.UploadFile, filePath);
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                try
                {
                    username = textBox1.Text;
                    password = textBox2.Text;
                    URLpath = textBox3.Text;
                    filePath = textBox4.Text;
                    ConnectAndUpload();
                    DateTime time = DateTime.Now;
                    label5.Text = DateTime.Now.ToString() + " Last Upload Occurred";
                }
                catch (Exception error)
                {
                    MessageBox.Show("FTP UPLOAD FAILED, Please check all your details are correct",
           "FTP UPLOAD FAILURE",
           MessageBoxButtons.OK,
           MessageBoxIcon.Error);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                
               
            }
    
            private void label5_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (textBox4.TextLength == 0)
                {
                    MessageBox.Show("No path in textbox",
           "Important Note",
           MessageBoxButtons.OK,
           MessageBoxIcon.Exclamation);
                }
                else
                {
                    listBox1.Items.Add(textBox4.Text);
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                try
                {
                    listBox1.Items.RemoveAt(Convert.ToInt32(label6.Text));
                }
    
                catch (Exception error)
                {
                    MessageBox.Show("No path selected",
            "Important Note",
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation);
                }
            }
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                label6.Text = listBox1.SelectedIndex.ToString();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        }
    
    }

All Replies

  • Saturday, September 01, 2012 1:15 PM
     
     

    Hi, when exacty do the files getting renames?

    And can you show us some example of it?

    thx


    Mitja

  • Saturday, September 01, 2012 1:28 PM
     
     
    I have no idea when they they are being renamed. They keep getting renamed to STOR instead of keeping their original filename
  • Saturday, September 01, 2012 1:42 PM
     
      Has Code

    They for sure dont rename just by them self - there must be something, or some code that does that. 

    When exactly is renamed? When here in your app, before send it, or later when checking it on server?

    ---

    One more thing: you are getting the file path from your textBox4 control. Is that right? You actually pass the full path  + file name to ftp.

    Why dont you retrevie file name only, by using FileInfo  (or File) classes?

                string filePath = textBox1.Text;
                string fileNameOnly = "";
                FileInfo fi;
                try
                {
                    fi = new FileInfo(filePath);
                    fileNameOnly = Path.GetFileName(fi.FullName);  //use fileNameOnly variable to pass it further to ftp.
                }
                catch
                {
                    throw new FileNotFoundException("Specified path does not include an appropriate folder`s path or file name.");
                }


    Mitja

  • Saturday, September 01, 2012 1:49 PM
     
     

    “STOR” is the value of ‘WebRequestMethods.Ftp.UploadFile’. Reconsider this value (e.g. specify an URL), or try another ‘Client.UploadFile’ that takes three arguments.

  • Saturday, September 01, 2012 1:51 PM
     
     
    When I look on the server its been renamed. Thanks for the above code, what I was thinking is if its a server related issue, I could do the upload and then force my app to rename it. The reason I'm getting it from the textbox is because I have now added a listbox where you can add more than one file to upload, so the application uses a for loop to iterate through the listbox uploading each file
  • Saturday, September 01, 2012 1:55 PM
     
     

    So how would I correct this exactly. A client uploadfile with 3 arguments ? Im guessing one of those arguments would be the filename ?
  • Saturday, September 01, 2012 3:29 PM
     
     Answered Has Code

    Try this:

    Client.UploadFile( Path.GetFileName( filePath ), filePath );

    • Marked As Answer by C-Sharp Mamba Sunday, September 02, 2012 11:19 AM
    •