动态添加上传控件

代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ClassifyInfo.WebForm1" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript">
        function AddAttachments() {
            document.getElementById("attach").innerText = "继续添加附件";
            //tb变量未加var在javascript中代表全局变量
            tb = document.getElementById("attachments");
            var newRow = tb.insertRow();
            newRow.insertCell().innerHTML = "<input type='file' size='50' name='File' />&nbsp;&nbsp;<input type='button' value='删除' οnclick='delFile(this.parentElement.parentElement.rowIndex)' />";
        }

        function delFile(index) {
            document.getElementById("attachments").deleteRow(index);
            tb.rows.length > 0 ? document.getElementById('attach').innerText = "继续添加附件" : document.getElementById("attach").innerText = "添加附件";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server"  enctype="multipart/form-data"  method="post">
    <div>
    <table  id="attachments"></table>
    <a id="attach" name="attach" href="javascript:;" style="font-family:宋体; font-size:9pt;" title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小" οnclick="AddAttachments()">添加附件</a>
    <br /><br /><br /><br /><br />
        <asp:Button ID="btnSend" runat="server" Text="上传" οnclick="btnSend_Click" />
    </div>
    </form>
</body>
</html>

 

 

代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> protected void btnSend_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            int attCount = 0;

            string filePath = "";

            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    filePath = Request.Files[i].FileName;
                    sb.Append("Files" + ++attCount + ": " + filePath + "<br>");
                    Request.Files[0].SaveAs(Server.MapPath("./") + filePath.Substring(filePath.LastIndexOf("\\") + 1));
                }
            }

            sb.Insert(0, "you upload " + attCount + " files.<br>");

            Response.Write(sb.ToString());


        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值