首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用C#上传文件到FTP

使用C#上传文件到FTP
EN

Stack Overflow用户
提问于 2013-03-07 18:31:14
回答 10查看 299.6K关注 0票数 125

我尝试用C#将文件上传到FTP服务器。文件已上载,但字节为零。

代码语言:javascript
运行
复制
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 !");
    }
}
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2014-10-15 00:23:10

现有的答案是有效的,但当WebClient已经很好地实现了WebRequest上传时,为什么还要重新发明轮子并为较低级别的FTP类型而烦恼:

代码语言:javascript
运行
复制
using (var client = new WebClient())
{
    client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}
票数 285
EN

Stack Overflow用户

发布于 2017-08-16 13:42:39

最简单的方法

使用.NET框架将文件上传到FTP服务器的最简单方法是使用WebClient.UploadFile method

代码语言:javascript
运行
复制
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.CopyToFileStream复制到FTP流

代码语言:javascript
运行
复制
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);
}

进度监控

如果您需要监控上传进度,您必须自己分块复制内容:

代码语言:javascript
运行
复制
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

有关递归上载的信息,请参见

Recursive upload to FTP server in C#

票数 46
EN

Stack Overflow用户

发布于 2013-03-07 19:54:48

.NET 5 Guide

代码语言:javascript
运行
复制
async Task<FtpStatusCode> FtpFileUploadAsync(string ftpUrl, string userName, string password, string filePath)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential(userName, password);

    using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    using (Stream requestStream = request.GetRequestStream())
    {
        await fileStream.CopyToAsync(requestStream);
    }

    using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
    {
        return response.StatusCode;
    }
}

.NET框架

代码语言:javascript
运行
复制
public void UploadFtpFile(string folderName, string fileName)
{

    FtpWebRequest request;

    string folderName; 
    string fileName;
    string absoluteFileName = Path.GetFileName(fileName);
    
    request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/{2}", "127.0.0.1", folderName, absoluteFileName))) as FtpWebRequest;
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = 1;
    request.UsePassive = 1;
    request.KeepAlive = 1;
    request.Credentials =  new NetworkCredential(user, pass);
    request.ConnectionGroupName = "group"; 

    using (FileStream fs = File.OpenRead(fileName))
    {
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(buffer, 0, buffer.Length);
        requestStream.Flush();
        requestStream.Close();
    }
}

如何使用

代码语言:javascript
运行
复制
UploadFtpFile("testFolder", "E:\\filesToUpload\\test.img");

在你的foreach中使用这个

而且你只需要创建一次文件夹

创建文件夹的步骤

代码语言:javascript
运行
复制
request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/", "127.0.0.1", "testFolder"))) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse ftpResponse = (FtpWebResponse)request.GetResponse();
票数 44
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15268760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档