using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.IO; namespace jquerytest.test1 { public partial class WebForm19 : System.Web.UI.Page { public static ArrayList files = new ArrayList(); protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { if (fupFile.HasFile) { ListItem item = new ListItem(); item.Value = item.Text = fupFile.PostedFile.FileName; if (!lbxFile.Items.Contains(item)) { lbxFile.Items.Add(item); files.Add(fupFile); } else Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<script>alert('不能添加已经添加过的文件!')</script>"); } } protected void btnPost_Click(object sender, EventArgs e) { if (files.Count > 0) { if (!Directory.Exists(MapPath("../files"))) Directory.CreateDirectory(MapPath("../files")); foreach (FileUpload fup in files) { if (fup.HasFile) fup.SaveAs(MapPath("../files") + "/" + fup.FileName);//无法访问已关闭的文件 } Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<script>alert('上传成功!')</script>"); } } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm19.aspx.cs" Inherits="jquerytest.test1.WebForm19" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td align="right"> 本地文件: </td> <td> <asp:FileUpload ID="fupFile" runat="server" CssClass="btn" Width="247px" Height="20px" onkeydown="event.returnValue=false;" onpaste="return false" /> </td> </tr> <tr> <td align="right"> 文件列表: </td> <td valign="top"> <asp:ListBox ID="lbxFile" runat="server" Height="145px" Width="245px" CssClass="txt"> </asp:ListBox> </td> </tr> <tr> <td colspan="5"> <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" /> <asp:Button ID="btnPost" runat="server" Text="上传" OnClick="btnPost_Click" /> </td> </tr> </table> </div> </form> </body> </html>
图