文件的上传与下载

动态添加上次按钮:

前台:

<head runat="server">
    <title>文件上传按钮的动态添加与删除</title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#addfile").click(function () {
                $("#files").append($("<div class='file'><input type='file' name='file' />&nbsp;&nbsp<input type='button' name='delete' value='删除' /></div>"));

                //删除
                $("#files").find("input[name='delete']").each(function (i) {
                    $(this).click(function () {
                        $(this).parent("div[class='file']").remove();
                    });
                });
            });
        });
       
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <div id="files">
        </div>
        <br />
        <br />
        <br />
        <div>
            <input type="button" id="addfile" value="新增附件" />&nbsp;&nbsp;&nbsp;&nbsp<asp:Button
                ID="btnUp" runat="server" Text="提交" OnClick="btnUp_Click" />
        </div>
    </div>
    </form>
</body>

 

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileLoad
{
    public partial class FileUpload2p : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            int num = Request.Files.Count;
           
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (!string.IsNullOrEmpty(Request.Files[i].FileName))
                {
                    //Request.Files[i].SaveAs("D:\\" + Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf("\\") + 1));
                    //Request.Files[i].SaveAs("E:\\" + Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf("\\") + 1));
                    Request.Files[i].SaveAs("D:\\" + Request.Files[i].FileName);
                }
            }
        }
    }
}

posted @ 2010-11-18 15:08  x喜德盛  阅读(173)  评论(0编辑  收藏  举报