如何使用Windows应用程序将文件上传到Web服务器/ ftp服务器c#

如何使用Windows应用程序将文件上传到Web服务器/ ftp服务器c#

问题描述:

我正在尝试使用Windows应用程序(c#)将文件上传到网络。我试过以下代码



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:

无法解析远程名称:'username'

The remote name could not be resolved: 'username'





任何人都可以指导我吗?

提前致谢。



Can anyone please guide me?
Thanks in advance.

尝试提供有效的用户名和密码FTP服务而不是用户名。
Try supplying a valid user name and password for the FTP service instead of "username".


http://www.vcskicks.com/csharp_ftp_upload .php [ ^ ]


使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Text;

使用System.Windows.Forms;

使用System .Threading;

usi ng System.IO;

使用TusharFtp;

使用System.Configuration;

使用System.Runtime.InteropServices;

namespace ftp_progress_bar

{

公共部分类Form1:表格

{

[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 =代表

{

label1.Text = b +out of of + a +left+(++ c)+%;

if(c< = 100)

{

progressBar1.Value = c;

}

};

this.BeginInvoke(m);






}

私人字符串用户,通行证,服务器,dirs;

private void tushar()

{

试试

{

MethodInvoker m1 =委托

{

button2.Text =上传;

};

this.BeginInvoke (m1);

TusharFtp.FtpClass t = new TusharFtp.FtpHelperClass();

t.FtpServer = servers;

t.FtpUserName =用户;

t.FtpPassword =通过;

t.FtpModeUsePassive = true;

t.uploadFile + = new TusharFtp.DisplayUploadDelegate(tushar1) ;

c = 0;

string h = targets.ToString();

string a = t.FtpFileUpload(targets.ToString(),如果(a.IndexOf(226)!= -1)

{try {

MethodInvoker m = delegate

{

string d = dirs.Replace(/, \\);

d = d.Replace(\\httpdocs \\,\\);



label1.Text = targets.Name +已成功上传;

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;

}

}

其他

{

试试

{

MethodInvoker m =委托

{

label1.Text = a;

button2.Text =upload;



textBox1.Text = string.Empty;

};

this.BeginInvoke(m);

}

catch(InvalidOperationException)

{

button2.Text =upload;

}

}



}

catch(NullReferenceException)

{

MessageBox.Show (有些字段是空的,呃ror,MessageBoxButtons.OK,MessageBoxIcon.Error);

}



}

私有FileInfo目标;

private void button1_Click(object sender,EventArgs e)

{

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

{





MessageBox.Show(等待上传完成! ,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(对象发送者,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 =新的ThreadStart(tushar);



System.Threading.Thread t1 = new System.Threading.Thread(th);

t1.Start ();

}

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

{





MessageBox.Show(等待上传完成!,错误,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);



}

else

{

MessageBox.Show(有些字段是空的! ,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(tru e);

}



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);

}

}

}
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);
}
}
}