c# ftp, keep original filename
-
Friday, August 31, 2012 1:47 PM
I am able to upload to an ftp server but it keeps changing the name for the file, how would I keep the original filename ?
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
-
Friday, August 31, 2012 1:56 PM
Try to use this overload of UploadFile
http://msdn.microsoft.com/it-it/library/ms144230(v=vs.80)
Regards,
Bubu
http://zsvipullo.blogspot.it
Please mark my answer if it helped you, I would greatly appreciate it. -
Friday, August 31, 2012 1:59 PM
can you try using FtpWebRequest http://msdn.microsoft.com/en-us/library/ms229715.aspx and try to write the content with stream.
regards
-
Saturday, September 01, 2012 6:37 PM
Probably your file name is the cause of this issue.
Check the name restrictions by your ftp server and take care that your file name complies with its name conventions.
Best,
wizend
-
Monday, September 03, 2012 6:43 AM
Since you're calling
Client.UploadFile(WebRequestMethods.Ftp.UploadFile, filePath);
you're uploading the file named in the filePath variable and uploading it as the named found in the UploadFile constant, that is "STOR".
Change the first parameter to the name you want instead. For example, Path.GetFileName(filePath)
- Marked As Answer by C-Sharp Mamba Tuesday, September 04, 2012 7:29 AM

