Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to upload a file to web using windows application(c#). I've tried this following code

private void uploadFile(string FTPAddress, string filePath, string username, string password)
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));

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

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

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

            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            MessageBox.Show("Uploaded Successfully");
        }


and getting this following error while trying this

Quote:
The remote name could not be resolved: 'username'


Can anyone please guide me?
Thanks in advance.
Posted
Updated 12-Feb-13 19:47pm
v2

Try supplying a valid user name and password for the FTP service instead of "username".
 
Share this answer
 
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using TusharFtp;
using System.Configuration;
using System.Runtime.InteropServices;
namespace ftp_progress_bar
{
public partial class Form1 : Form
{
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);


public static void ShowToFront(string windowName)
{
IntPtr firstInstance = FindWindow(null, windowName);
ShowWindow(firstInstance, 1);
SetForegroundWindow(firstInstance);
}

public Form1()
{
InitializeComponent();

}
private int c;
public void tushar1(long a, long b)
{

MethodInvoker m = delegate
{
label1.Text = b + " out of " + a + " left " + (++c) + "%";
if (c <= 100)
{
progressBar1.Value = c;
}
};
this.BeginInvoke(m);



}
private string users, pass, servers, dirs;
private void tushar()
{
try
{
MethodInvoker m1 = delegate
{
button2.Text = "uploading";
};
this.BeginInvoke(m1);
TusharFtp.FtpClass t = new TusharFtp.FtpHelperClass();
t.FtpServer = servers;
t.FtpUserName = users;
t.FtpPassword = pass;
t.FtpModeUsePassive = true;
t.uploadFile += new TusharFtp.DisplayUploadDelegate(tushar1);
c = 0;
string h = targets.ToString();
string a = t.FtpFileUpload(targets.ToString(), dirs);
if (a.IndexOf("226 ") != -1)
{try{
MethodInvoker m = delegate
{
string d= dirs.Replace("/","\\");
d = d.Replace("\\httpdocs\\", "\\");

label1.Text = targets.Name + " has been succesfully uploaded";
richTextBox1.AppendText("\n http:\\\\" + t.FtpServer + d + targets.Name);
progressBar1.Value = 100;
button2.Text = "upload";
textBox1.Text = string.Empty;

};

this.BeginInvoke(m);
}
catch(InvalidOperationException ins)
{
button2.Text = "upload";

textBox1.Text = string.Empty;
}
}
else
{
try
{
MethodInvoker m = delegate
{
label1.Text = a;
button2.Text = "upload";

textBox1.Text = string.Empty;
};
this.BeginInvoke(m);
}
catch(InvalidOperationException)
{
button2.Text = "upload";
}
}

}
catch (NullReferenceException )
{
MessageBox.Show("some fields are left empty","error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

}
private FileInfo targets;
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "uploading" && textBox1.Text != string.Empty)
{


MessageBox.Show("wait for the upload to complete!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}
else
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
targets = new FileInfo(textBox1.Text);
}
}

}

private void button2_Click(object sender, EventArgs e)
{
users = ConfigurationManager.AppSettings["usersname"].ToString();
pass= ConfigurationManager.AppSettings["userspass"].ToString();
servers = ConfigurationManager.AppSettings["userserver"].ToString();
dirs = ConfigurationManager.AppSettings["userdir"].ToString();


if (button2.Text == "upload" && textBox1.Text!=string.Empty)
{

ThreadStart th = new ThreadStart(tushar);

System.Threading.Thread t1 = new System.Threading.Thread(th);
t1.Start();
}
else if (button2.Text == "uploading" && textBox1.Text != string.Empty)
{


MessageBox.Show("wait for the upload to complete!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}
else
{
MessageBox.Show("some fields are empty!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
button2.Text = "upload";

}

}

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
this.ShowInTaskbar = true;
}

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{

components.Dispose();


base.Dispose(true);
}

private void options(object sender, EventArgs e)
{
optionsdialog opts = new optionsdialog();
opts.Show();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
components.Dispose();


base.Dispose(true);
}
}
}
 
Share this answer
 
Comments
CHill60 9-Jul-14 9:39am    
Apart from the fact that you haven't formatted the code, and apart from the fact you haven't even attempted to solve the OPs problem, this post is over 18 months old and has an accepted solution.
Everything you have posted has been copied from this article Simple FTP library in C#[^]. You've missed out most of the code, but (if you were answering a relevant question) you should have posted a link to the article, not tried to claim this as your own work.

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