You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C#将生成的XML文件上传至Azure服务器的FTP文件夹?

用C#上传生成的XML到Azure FTP文件夹的几种实用实现方式

我来给你整理几个实际项目里常用的方法,都是经过验证可行的:

方法一:用.NET原生的FtpWebRequest直接对接FTP

这是最传统的方式,不需要额外依赖,适合标准的Azure FTP端点。步骤很简单:先生成XML内容,再通过FTP请求上传。

先看生成XML的示例(用XDocument,你也可以用XmlSerializer或者其他方式):

// 生成简单的XML文档
XDocument xmlDoc = new XDocument(
    new XElement("Root",
        new XElement("Product", "Azure FTP Upload Demo"),
        new XElement("CreatedTime", DateTime.Now.ToUniversalTime().ToString("o"))
    )
);
string xmlContent = xmlDoc.ToString();

然后是上传到Azure FTP的代码:

string ftpUrl = "ftp://your-storage-account.ftp.core.windows.net/your-folder/demo.xml";
string ftpUsername = "your-storage-account\\your-ftp-username"; // Azure FTP用户名格式是「存储账户名\用户名」
string ftpPassword = "your-ftp-password-or-storage-key";

// 创建FTP上传请求
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(ftpUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
uploadRequest.UsePassive = true; // Azure FTP默认要求被动模式,一定要开启

// 把XML内容转成字节数组
byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlContent);
uploadRequest.ContentLength = xmlBytes.Length;

// 写入数据到FTP流
using (Stream requestStream = uploadRequest.GetRequestStream())
{
    requestStream.Write(xmlBytes, 0, xmlBytes.Length);
}

// 确认上传结果
using (FtpWebResponse response = (FtpWebResponse)uploadRequest.GetResponse())
{
    Console.WriteLine($"上传完成,服务器返回:{response.StatusDescription}");
}

注意点:

  • Azure FTP的端点一般是ftp://<存储账户名>.ftp.core.windows.net
  • 用户名格式必须是「存储账户名\FTP用户名」,不能直接用FTP用户名
  • 被动模式必须开启,否则可能连接失败

方法二:用Azure Blob Storage SDK上传(如果FTP绑定到Blob存储)

很多时候Azure的FTP服务是和Blob Storage绑定的——也就是说你通过FTP上传的文件其实是存在Blob容器里的。这种情况下用Azure官方的SDK会更靠谱,还能利用Azure的重试、托管身份等特性。

首先要安装NuGet包Azure.Storage.Blobs,然后看代码:

// 先生成XML(和上面一样)
XDocument xmlDoc = new XDocument(
    new XElement("Root",
        new XElement("Product", "Azure Blob FTP Demo"),
        new XElement("CreatedTime", DateTime.Now.ToUniversalTime().ToString("o"))
    )
);
byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlDoc.ToString());

// 初始化Blob客户端
string storageConnectionString = "your-storage-account-connection-string";
string containerName = "your-container"; // 对应FTP绑定的容器
string blobPath = "your-folder/demo.xml"; // 对应FTP里的文件夹路径

BlobServiceClient blobClient = new BlobServiceClient(storageConnectionString);
BlobContainerClient containerClient = blobClient.GetBlobContainerClient(containerName);
BlobClient targetBlob = containerClient.GetBlobClient(blobPath);

// 上传XML字节流
using (MemoryStream stream = new MemoryStream(xmlBytes))
{
    await targetBlob.UploadAsync(stream, overwrite: true); // overwrite设为true会覆盖已存在的文件
}

Console.WriteLine("XML已上传到Blob Storage,同时可以通过FTP访问啦");

优势:

  • 不用处理FTP的底层细节,SDK自动管理连接和重试
  • 支持托管身份认证,不用硬编码密码或连接字符串,更安全
  • 上传的文件同时可以通过Blob Storage API和FTP访问,灵活度高

方法三:用第三方库FluentFTP实现更灵活的上传

如果需要批量上传、进度跟踪或者支持FTPS加密传输,可以用FluentFTP这个第三方库,它比原生的FtpWebRequest好用很多。

先安装NuGet包FluentFTP,然后代码示例:

// 生成XML内容
XDocument xmlDoc = new XDocument(
    new XElement("Root",
        new XElement("Product", "FluentFTP Demo"),
        new XElement("CreatedTime", DateTime.Now.ToUniversalTime().ToString("o"))
    )
);
byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlDoc.ToString());

// 连接Azure FTP服务器
string ftpHost = "your-storage-account.ftp.core.windows.net";
int ftpPort = 21; // 显式FTPS用21,隐式FTPS用990
string ftpUsername = "your-storage-account\\your-ftp-username";
string ftpPassword = "your-ftp-password";

using (FtpClient ftpClient = new FtpClient(ftpHost, ftpPort, ftpUsername, ftpPassword))
{
    ftpClient.Connect();
    // 上传字节数组到指定路径
    ftpClient.UploadBytes(xmlBytes, "/your-folder/demo.xml");
    ftpClient.Disconnect();
}

Console.WriteLine("用FluentFTP上传成功!");

额外特性:

  • 支持FTPS/SFTP加密传输,保护数据安全
  • 可以监听上传进度事件,实时显示上传百分比
  • 支持批量上传、文件夹创建等复杂操作

几个关键提醒

  1. Azure FTP配置要到位:确保你的存储账户已经启用了FTP/FTPS服务,并且给FTP用户分配了正确的权限;如果是Blob绑定的FTP,容器的访问级别要设置合适。
  2. 优先用加密传输:尽量用FTPS(FTP over SSL/TLS)而不是明文FTP,Azure支持两种FTPS模式,配置时选对应的端口即可。
  3. 添加错误处理:实际代码里一定要加异常捕获,比如处理连接失败、权限不足、文件过大等情况,保证程序的健壮性。
  4. 大文件分块上传:如果XML文件很大,建议用分块上传,Azure SDK和FluentFTP都支持这个功能,避免内存占用过高。

内容的提问来源于stack exchange,提问作者Alma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:42:40