📅  最后修改于: 2020-12-27 13:35:29             🧑  作者: Mango
ASP.NET FileUpload控件提供AllowMultiple属性以将多个文件上传到服务器。此属性为true或false。
本示例包含以下文件。
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="UploadMultipleFilesExample.aspx.cs" Inherits="UploadMultipleExample.UploadMultipleFilesExample" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UploadMultipleExample
{
public partial class UploadMultipleFilesExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
var count = 0;
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
uploadedFile.SaveAs(SaveLocation);
count++;
}
catch (Exception ex)
{
FileUploadStatus.Text = "Error: " + ex.Message;
}
}
if (count > 0)
{
FileUploadStatus.Text = count + " files has been uploaded.";
}
}
else
{
FileUploadStatus.Text = "Please select a file to upload.";
}
}
}
}
输出:
选择2个文件进行上传
最初,看到上传文件夹为空。
将文件上传到服务器。
现在,查看上传文件夹。它包含上传的两个文件。