使用ASP.NET上傳多個文件到服務器

在Email系統中經常會上傳多個文件到服務器,用戶大多習慣一次上傳所有的文件,而不是逐個上傳,我們可以使用javascript動態地添加file元素到表單,然後在服務器端處理這些file

效果圖如下:

多文件上傳

頁面代碼MutlileFileUpload.aspx如下:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MutlileFileUpload.aspx.cs"
    Inherits="MutlileFileUpload" %>

<!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>
    <title>多文件上傳到服務器Demo</title>
    <link href="css/writemail.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
<!--
        var MAXFILES = 5;        //文件計數器       
        var fileCount = 0;
        function addAttach(noAlert) {
            if (fileCount >= MAXFILES && !noAlert) { alert("最多隻能添加" + MAXFILES + "個附件!"); return; }

            var fileSectionDiv = document.getElementById("files");
            var fileItemDiv = document.createElement("div");
            fileCount++;
            var content = "<input type='file' οnchange='return addAttach(true);' name='fileUpload'" + fileCount + "> <a href='#' οnclick='return delAttach(\"" + fileCount + "\")' class='delete_attach' >移除附件</a>";
            fileItemDiv.id = "fileItemDiv" + fileCount;
            fileItemDiv.innerHTML = content;
            fileSectionDiv.appendChild(fileItemDiv); 
             return false;
         }

         function delAttach(fileIndex) {
             var fileSectionDiv = document.getElementById("files");
             var fileItemDiv = document.getElementById("fileItemDiv" + fileIndex);
             fileSectionDiv.removeChild(fileItemDiv);
             fileCount--;
             return false; 
        }    // 
 --></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a id="addAttach_a" οnclick="return addAttach(false);" href="#"  class="add_attach">添加附件</a>
        <div id="files">
        </div>
        <asp:Button ID="btnSend" runat="server" Text="發送" OnClick="btnSend_Click" />
    </div>
    </form>
</body>
</html>

 

樣式表WriteMail.css代碼如下:

.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如下:

 

using System;
using System.IO;
using System.Web.UI;

public partial class MutlileFileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {        //告訴表單如何格式化文件信息 
        Page.Form.Enctype = "multipart/form-data";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        for (int index = 0; index < Request.Files.Count; index++)
        {
            if (!string.IsNullOrEmpty(Request.Files[index].FileName))
            {
                Request.Files[index].SaveAs(Path.Combine(Server.MapPath("Files"), System.IO.Path.GetFileName(Request.Files[index].FileName)));
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章