Hopes

Start Here..

 

asp.net,c#,同时上传多个文件

 

asp.net,c#,同时上传多个文件

2011-08-19 10:52 17人阅读 评论(0) 收藏 举报

ASPX页:

<head runat="server">
    <title>多文件上传</title>
    <script type="text/javascript" language="javascript">
        function addForm()
        {
            var strForm ="<input type='file' size='50' name='File' />"
            document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",strForm)
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <p id="MyFile">
          <input type="file" size="50" name="File" />
    </p>
    <p>
          <input type="button" value="增加一个" onclick="addForm()" />
          
          <asp:Button Runat="server" Text="开始上传" ID="UploadButton" 
                onclick="UploadButton_Click">
          </asp:Button>

          
          <input onclick="this.form.reset()" type="button" value="重 置" />
          <br />
          <asp:Label ID="Label1" runat="server"></asp:Label>
    </p>
    </form>
</body>
</html>

cs页:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        this.SaveFiles();
    }
    private void SaveFiles()
    {
        //遍历表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;

        //状态信息
        string strout = "<br>上传的文件分别是:<hr color=red><table style='width: 500px;'>";
        strout += "<tr><td>文件类型</td><td>客户端地址</td><td>上传文件名</td><td>扩展名</td></tr>";
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                //访问单独文件
                HttpPostedFile postedFile = files[iFile];
                string fileName, fileExtension;

                fileName = System.IO.Path.GetFileName(postedFile.FileName);

                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName);

                    strout += "<tr><td>" + postedFile.ContentType.ToString() + "</td>";
                    strout += "<td>" + postedFile.FileName + "</td>";
                    strout += "<td>" + fileName + "</td>";
                    strout += "<td>" + fileExtension + "</td></tr>";

                    postedFile.SaveAs(Server.MapPath("uploadFile/") + fileName);
                }
                else 
                {
                    strout = "<br>请您选择一个文件!!!";
                }
            }

            Label1.Text = strout.ToString();

        }
        catch (Exception Ex)
        {
            Label1.Text = Ex.Message.ToString();
        }
    }
}

posted on 2012-08-19 20:42 ** 阅读(913) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

你好!

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

新闻档案

相册

收藏夹

C#学习

友情链接

搜索

最新评论

阅读排行榜

评论排行榜