• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

asp.net多文件上传使用html控件的File控件,在form中就需要加入【 enctype="multipart/form-data"】。

up3.aspx文件代码

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

<!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 language="javascript" type="text/javascript">
        function addFile() {

            var odiv = document.getElementById("MyFile");

            var str = "<div><input name='File' type='file'  /></div>";

            odiv.insertAdjacentHTML("beforeEnd", str);

        }
        function resetFile() {

            var odiv = document.getElementById("MyFile");

            odiv.innerHTML = "<div><input name='File' type='file' /></div>";

        }
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <input type="button" value="增加" onclick="addFile()" />
    <input type="button" value="重置" onclick="resetFile()" />
    <div id="MyFile">
        <div><input name="File" type="file" /></div>
    </div>
    <asp:Button runat="server" Text="上传" ID="Button1" OnClick="Button1_Click" BorderColor="Desktop"
        BorderWidth="1px" Height="20px" Width="60px"></asp:Button>
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

up3.aspx.cs文件代码

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

using System.IO;

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

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string upPath = "/up/";  //上传文件路径
        int upLength = 5;        //上传文件大小
        string upFileExtName = "|bmp|jpg|jpeg|png|gif|";


        HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

        int flag = _files.Count;
        int flagN = 0;
        int flagE = 0;
        int flagEE = 0;
        string flagEEstr = "";

        for (int i = 0; i < _files.Count; i++)
        {
            string name = _files[i].FileName;

            FileInfo fi = new FileInfo(name);

            string oldfilename = fi.Name;
            string scExtension = fi.Extension.ToLower();

            string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + fi.Extension; // 文件名称,当前时间(yyyyMMddhhmmssfff)
            string webFilePath = Server.MapPath(upPath) + fileName;        // 服务器端文件路径

            if (upFileExtName.IndexOf(scExtension.Replace(".", "")) == -1)
            {
                flagEE = flagEE + 1;
                flagEEstr = flagEEstr + "" + (i + 1) + "个文件,文件名[" + oldfilename + "],文件类型不符合!";
                continue;
            }

            if ((fi.Length / (1024 * 1024)) > upLength)
            {
                flagEE = flagEE + 1;
                flagEEstr = flagEEstr + "" + (i + 1) + "个文件,文件名[" + oldfilename + "],超出" + upLength + "M大小限制!";
                continue;
            }

            try
            {
                _files[i].SaveAs(webFilePath);
            }
            catch (Exception ex)
            {
                flagEE = flagEE + 1;
                flagEEstr = flagEEstr + "" + (i + 1) + "个文件,上传异常【"+ex.Message+"";
               
            }

        }

        Label1.Text = "总文件【" + flag + "】,上传成功文件【" + flagN + "】,异常文件【" + (flagE + flagEE) + "】【" + flagEEstr + "";
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap