VB.NET/C#批量上传文件至服务器的最优方案探讨
文件上传方案选择与实现(VB.NET/C#)
场景说明
我们有一个uploads.aspx页面,包含名为“Files To Upload”的多行文本框和“Upload”按钮。用户可在文本框中粘贴本地文件路径,示例如下:
/pages/contact-us.aspx
/pages/contact-us.aspx.vb
/App_Code/Contact/contact.vb
/pages/css/contact.css
/pages/css/contact2.css
核心问题:使用VB.NET/C#实现这些文件的上传,最优方式是FTP还是HTTP?目前很多旧代码示例已被弃用或不推荐,需要基于当前.NET规范的方案。
方案对比与推荐实现
1. HTTP上传:优先推荐(现代.NET首选)
.NET官方已弃用HttpWebRequest、WebClient等旧API,当前推荐使用HttpClient,这是跨.NET Framework、.NET Core/.NET 5+的统一HTTP客户端,支持异步、连接池、现代HTTP特性。
实现思路
- 读取文本框中的所有文件路径,验证文件存在性
- 使用
HttpClient构建MultipartFormDataContent,将文件添加到请求中 - 发送POST请求到接收文件的HTTP接口
C# 示例代码
using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; using System.Web.UI; public partial class uploads : Page { private static readonly HttpClient _httpClient = new HttpClient(); protected async void UploadButton_Click(object sender, EventArgs e) { var filePaths = FilesToUpload.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var content = new MultipartFormDataContent(); foreach (var path in filePaths) { var fullPath = Server.MapPath(path); // 若路径是站点相对路径,转换为物理路径 if (!File.Exists(fullPath)) continue; var fileStream = File.OpenRead(fullPath); content.Add(new StreamContent(fileStream), "files", Path.GetFileName(fullPath)); } try { // 替换为目标HTTP接收接口地址 var response = await _httpClient.PostAsync("https://your-target-server/upload", content); response.EnsureSuccessStatusCode(); // 处理上传成功逻辑 } catch (HttpRequestException ex) { // 处理异常 } } }
VB.NET 示例代码
Imports System Imports System.IO Imports System.Net.Http Imports System.Threading.Tasks Imports System.Web.UI Partial Class uploads Inherits Page Private Shared ReadOnly _httpClient As New HttpClient() Protected Async Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click Dim filePaths = FilesToUpload.Text.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) Dim content = New MultipartFormDataContent() For Each path In filePaths Dim fullPath = Server.MapPath(path) ' 转换站点相对路径为物理路径 If Not File.Exists(fullPath) Then Continue For Dim fileStream = File.OpenRead(fullPath) content.Add(New StreamContent(fileStream), "files", Path.GetFileName(fullPath)) Next Try ' 替换为目标HTTP接收接口地址 Dim response = Await _httpClient.PostAsync("https://your-target-server/upload", content) response.EnsureSuccessStatusCode() ' 处理上传成功逻辑 Catch ex As HttpRequestException ' 处理异常 End Try End Sub End Class
2. FTP上传:仅当目标服务器仅支持FTP时使用
.NET官方已将FtpWebRequest标记为过时,不再推荐使用原生API。如果必须使用FTP,优先选择第三方成熟库,比如WinSCP.NET或Flurl.Http,这些库维护更活跃,支持现代FTP特性(如FTPS、SFTP)。
基于WinSCP.NET的示例(C#)
using WinSCP; protected void UploadButton_Click(object sender, EventArgs e) { var filePaths = FilesToUpload.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = "your-ftp-server", UserName = "ftp-user", Password = "ftp-password" }; using (var session = new Session()) { session.Open(sessionOptions); foreach (var path in filePaths) { var fullPath = Server.MapPath(path); if (!File.Exists(fullPath)) continue; // 上传到FTP服务器的目标路径,需根据实际情况调整 session.PutFiles(fullPath, "/remote-target-folder/*").Check(); } } }
方案选择建议
- 优先选HTTP:如果目标服务器提供HTTP/HTTPS文件接收接口,
HttpClient是现代.NET的标准方案,API简洁、性能优异,且无过时风险。 - 仅用FTP:仅当目标服务器只支持FTP协议时使用,务必选择第三方库替代过时的
FtpWebRequest,避免后续.NET版本兼容性问题。
内容的提问来源于stack exchange,提问作者user1946932
相关产品推荐
相关产品推荐

