neatUpload是asp.net 中可以同时上传多个文件的控件,主页:http://neatupload.codeplex.com/。
效果如下图(显示有点不正常。。。):
使用步骤:
1. 在aspx的开始加上引用的dll,以及在html代码中添加<Upload:MultiFile ID="MultiFile1" UseFlashIfAvailable="true" runat="server" ViewStateMode="Enabled"></Upload:MultiFile>。
注意:一定要把 UseFlashIfAvailable设置为"true"
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="ResourcesAddEdit.aspx.vb" Inherits="Manage_WebPages_Research_ResourcesAddEdit" %> <%@ Register Assembly="Brettle.Web.NeatUpload" Namespace="Brettle.Web.NeatUpload" TagPrefix="Upload" %> <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> <!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 id="Head1" runat="server"> <title>管理</title> </head> <body> <div > <Upload:MultiFile ID="MultiFile1" UseFlashIfAvailable="true" runat="server" ViewStateMode="Enabled"></Upload:MultiFile> </div> </body>
2. 后台代码,公司用的是vb,为了照顾老员工。。
For Each item As Brettle.Web.NeatUpload.UploadedFile In MultiFile1.Files ' 上传文件。 Try '上传之前的名字 Dim oldFileName As String = item.FileName '获取上传文件的文件名,包括后缀 Dim ExtenName As String = System.IO.Path.GetExtension(oldFileName) '要存在硬盘上的新文件名 Dim savedFileName As String = oldFileName.Substring(0, oldFileName.IndexOf("."c)) & "_" & DateTime.Now.ToString("yyyyMMddhhmm") & ExtenName '组合成要存储的文件名和目录 Dim fileURL As String = Path.Combine(basePath, savedFileName) If Not Directory.Exists(basePath) Then Directory.CreateDirectory(basePath) End If item.SaveAs(fileURL) Catch generatedExceptionName As Exception Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "", "alert('error')") Return End Try Next
3. 如果实际运行的时候发现不能选择多个,则在web.config中设置一下
<configSections> <section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler,Brettle.Web.NeatUpload" allowLocation="true"/> </configSections> <neatUpload useHttpModule="true" maxRequestLength="2097151" maxNormalRequestLength="4096" xmlns="http://www.brettle.com/neatupload/config/2008"/> <appSettings/>
<httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload"/> </httpModules>