我尝试用C#将文件上传到FTP服务器。文件已上载,但字节为零。
private void button2_Click(object sender, EventArgs e)
{
var dirPath = @"C:/Documents and Settings/sander.GD/Bureaublad/test/";
ftp ftpClient = new ftp("ftp://example.com/", "username", "password");
string[] files = Directory.GetFiles(dirPath,"*.*");
var uploadPath = "/httpdocs/album";
foreach (string file in files)
{
ftpClient.createDirectory("/test");
ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
}
if (string.IsNullOrEmpty(txtnaam.Text))
{
MessageBox.Show("Gelieve uw naam in te geven !");
}
}
发布于 2014-10-15 00:23:10
现有的答案是有效的,但当WebClient
已经很好地实现了WebRequest
上传时,为什么还要重新发明轮子并为较低级别的FTP类型而烦恼:
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}
发布于 2017-08-16 13:42:39
最简单的方法
使用.NET框架将文件上传到FTP服务器的最简单方法是使用WebClient.UploadFile
method
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
高级选项
如果您需要更好控制,则使用FtpWebRequest
,但WebClient
不提供(如TLS/SSL encryption、ascii/文本传输模式、活动模式、传输恢复、进度监控等)。简单的方法是使用Stream.CopyTo
将FileStream
复制到FTP流
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}
进度监控
如果您需要监控上传进度,您必须自己分块复制内容:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
byte[] buffer = new byte[10240];
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
ftpStream.Write(buffer, 0, read);
Console.WriteLine("Uploaded {0} bytes", fileStream.Position);
}
}
有关图形用户界面进度(WinForms ProgressBar
)的信息,请参阅位于以下位置的C#示例:
How can we show progress bar for upload with FtpWebRequest
上传文件夹
如果要上载文件夹中的所有文件,请参见
Upload directory of files to FTP server using WebClient。
有关递归上载的信息,请参见
发布于 2013-03-07 18:37:39
下面的方法对我很有效:
public virtual void Send(string fileName, byte[] file)
{
ByteArrayToFile(fileName, file);
var request = (FtpWebRequest) WebRequest.Create(new Uri(ServerUrl + fileName));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
request.Credentials = new NetworkCredential(UserName, Password);
request.ContentLength = file.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(file, 0, file.Length);
requestStream.Close();
var response = (FtpWebResponse) request.GetResponse();
if (response != null)
response.Close();
}
您不能在您的代码中读取send文件参数,因为它只是文件名。
使用以下内容:
byte[] bytes = File.ReadAllBytes(dir + file);
来获取文件,以便可以将其传递给Send
方法。
https://stackoverflow.com/questions/15268760
复制相似问题