在Email系统中经常会上传多个文件到服务器,用户大多习惯一次上传所有的文件,而不是逐个上传,我们可以使用javascript动态地添加file元素到表单,然后在服务器端处理这些file

效果图如下:

页面代码MutlileFileUpload.aspx如下:

[html]  view plain copy

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MutlileFileUpload.aspx.cs"
  2. Inherits="MutlileFileUpload" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <title>多文件上传到服务器Demo</title>
  7. <link href="css/writemail.css" rel="stylesheet" type="text/css" />
  8. <script type="text/javascript">
  9. <!--
  10. var MAXFILES = 5;        //文件计数器
  11. var fileCount = 0;
  12. function addAttach(noAlert) {
  13. if (fileCount >= MAXFILES && !noAlert) { alert("最多只能添加" + MAXFILES + "个附件!"); return; }
  14. var fileSectionDiv = document.getElementById("files");
  15. var fileItemDiv = document.createElement("div");
  16. fileCount++;
  17. var content = "<input type='file' onchange='return addAttach(true);' name='fileUpload'" + fileCount + "> <a href='#' onclick='return delAttach(\"" + fileCount + "\")' class='delete_attach' >移除附件</a>";
  18. fileItemDiv.id = "fileItemDiv" + fileCount;
  19. fileItemDiv.innerHTML = content;
  20. fileSectionDiv.appendChild(fileItemDiv);
  21. return false;
  22. }
  23. function delAttach(fileIndex) {
  24. var fileSectionDiv = document.getElementById("files");
  25. var fileItemDiv = document.getElementById("fileItemDiv" + fileIndex);
  26. fileSectionDiv.removeChild(fileItemDiv);
  27. fileCount--;
  28. return false;
  29. }    //
  30. --></script>
  31. </head>
  32. <body>
  33. <form id="form1" runat="server">
  34. <div>
  35. <a id="addAttach_a" onclick="return addAttach(false);" href="#"  class="add_attach">添加附件</a>
  36. <div id="files">
  37. </div>
  38. <asp:Button ID="btnSend" runat="server" Text="发送" OnClick="btnSend_Click" />
  39. </div>
  40. </form>
  41. </body>
  42. </html>

样式表WriteMail.css代码如下:

[css]  view plain copy

 
  1. .delete_attach {PADDING-LEFT: 18px; BACKGROUND: url(../images/deleteattch_icon.gif) no-repeat left top; MARGIN-LEFT: 7px; WIDTH: 90px; COLOR: #002f76}.add_attach {PADDING-LEFT: 22px; BACKGROUND: url(../images/attach.gif) no-repeat left center; WIDTH: 90px; COLOR: #002f76}

后台代码MutlileFileUpload.aspx.cs如下:

[csharp] 
view plain
copy

 

  1. using System;
  2. using System.IO;
  3. using System.Web.UI;
  4. public partial class MutlileFileUpload : System.Web.UI.Page
  5. {
  6. protected void Page_Load(object sender, System.EventArgs e)
  7. {        //告诉表单如何格式化文件信息
  8. Page.Form.Enctype = "multipart/form-data";
  9. }
  10. protected void btnSend_Click(object sender, EventArgs e)
  11. {
  12. for (int index = 0; index < Request.Files.Count; index++)
  13. {
  14. if (!string.IsNullOrEmpty(Request.Files[index].FileName))
  15. {
  16. Request.Files[index].SaveAs(Path.Combine(Server.MapPath("Files"), System.IO.Path.GetFileName(Request.Files[index].FileName)));
  17. }
  18. }
  19. }
  20. }

最新文章

  1. Linux常用命令及shell脚本
  2. Jenkins的配置(rpm red hat方式)
  3. Oracle定时计划快速使用
  4. Webservice 65535 错误
  5. [游戏模版6] Win32 graph
  6. mysql5.5 uuid做主键与int做主键的性能实测
  7. Unity中启动VS时出现&quot;Visual Studio 2010 Shell 无效的许可证数据&quot;的解决办法
  8. Revit 二次开发 (二) 倾斜的板
  9. 【暑假】[深入动态规划]UVAlive 3983 Robotruck
  10. button 垂直分布
  11. Python自动化运维之26、Web框架本质、MVC与MTV
  12. 修改Macros的值
  13. 在Vue框架中使用百度地图
  14. TCP与UDP各自优缺点与区别
  15. IE兼容性小结(IE7及以上版本)
  16. [转] 利用BLKTRACE分析IO性能
  17. wampserver3.1.0安装及配置
  18. Linux中的黑洞(black hole)-/dev/null
  19. 在ubuntu14.4里编译UBOOT出错
  20. sass函数:@function

热门文章

  1. POJ 1741 树分治
  2. python中后端数据序列化不显示中文的解决方法
  3. spring核心及常用技术
  4. Java 请求webServce接口 带参数
  5. fpdf中文乱码,添加字符集
  6. [转]JSONObject,JSONArray使用手册
  7. uoj 67 新年的毒瘤 tarjan求割点
  8. SqlServer Base64解码中文
  9. MYSQL学习笔记 (一)
  10. mmm-master漂移问题的分析