这是一个非常简单的示例,可以帮助您入门。文件中的代码(使用从这里.aspx
借来的 jquery ):
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="UploadMany.aspx.cs" Inherits="UploadMany" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<script type="text/javascript">
var currentIndex = 1;
function appendOneUpload() {
$('form input:file').last().after($('<br /><input type="file" name="fileUpload' + currentIndex + '" onchange="appendOneUpload();"/>'));
currentIndex++;
}
</script>
<input type="file" runat="server" id="fileUpload0" onchange="appendOneUpload();"/> <br />
<asp:Button ID="uploadButton" runat="server" OnClick="uploadButton_Clicked" />
</asp:Content>
文件中的相应代码.aspx.cs
:
public partial class UploadMany : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void uploadButton_Clicked(object sender, EventArgs e)
{
var allFiles = Request.Files;
for (int fileIndex = 0; fileIndex < allFiles.Count; fileIndex++)
{
HttpPostedFile oneFile = allFiles[fileIndex];
if (oneFile.ContentLength > 0)
{
//do something with each uploaded file here
int i = 0;
}
}
}
}