前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script src="JQuery/jquery.js" type="text/javascript" language="javascript"></script>
<script src="JQuery/jquery.MultiFile.js" type="text/javascript" language="javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<asp:FileUpload ID="FileUpload1" runat="server" class="multi" maxlength="2" />
<asp:Button ID="btnUpload" runat="server" Text="Button" onclick="btnUpload_Click" />
</fieldset>
</div>
</form>
</body>
</html>
后台代码:protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("MyFile") + "\\"
+ System.IO.Path.GetFileName(hpf.FileName));
Response.Write("<script>alert('文件上传成功!')</script>");
}
}
}
catch (Exception ex)
{
throw ex;
}
}
效果图:
Jquery plugin下载:/Files/ly5201314/multiple-file-upload.zip,
只需要引用两个js文件:jquery.js;jquery.MultiFile.js。class="multi" maxlength="2" 是关键代码。