使用ASP.NET上传多个文件到服务器

时间:2023-03-09 19:16:43
使用ASP.NET上传多个文件到服务器

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

效果图如下:

使用ASP.NET上传多个文件到服务器

页面代码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. }