net实现多文件上传代码

net实现多文件上传代码,单个文件上传可能很容易作,但是很多朋友就是不是多文件上传,其它多文件上传和单文件没什么本质上的区别了,好我们先看看下面的文章就知道了.

protected bool upMorefile()
    {
       
        //遍历File表单元素
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        //状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder("成功上传的文件信息分别为:<hr color=red>");
        int fileCount;
        int filecount = files.Count;
       
      
        try
        {
            for (fileCount = 0; fileCount < files.Count; fileCount++)
            {
                //定义访问客户端上传文件的对象
                System.Web.HttpPostedFile postedFile = files[fileCount];
                string FileType = postedFile.ContentType.ToString();//获取要上传的文件类型,验证文件头 

                string fileName, fileExtension;
                //取得上传得文件名
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                //取得文件的扩展名
                fileExtension = System.IO.Path.GetExtension(fileName);

                //在上传文件不为空的情况下,验证文件名以及大小是否符合,如果不符合则不允许上传
                if (((FileType == "text/plain" && fileExtension.ToLower() == ".txt") || (FileType == "application/x-zip-compressed" && fileExtension.ToLower() == ".zip") || (FileType == "application/octet-stream" && fileExtension.ToLower() == ".rar"))&&postedFile.ContentLength/1024<=1024)
                {//在这里通过检查文件头与文件名是否匹配 从而限制了文件上传类型  注:可上传的类型有TXT,ZIP,RAR,且大小只能为1M一下
                   
                    if (fileName != String.Empty)
                    {
                        fileName = RandomFileName() + fileExtension;
                       
                        //上传的文件信息
                        strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                        strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                        strMsg.Append("上传文件的文件名:" + fileName + "<br>");
                        strMsg.Append("上传文件的大小为:" + postedFile.ContentLength + "字节<br>");
                        strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
                        //保存到指定的文件夹
                        postedFile.SaveAs(Server.MapPath("public_file/" + UserName + "/") + fileName);
                        fileName = "";

                    }
                }
                else
                {
                    strStatus.Text+= "第"+(fileCount+1)+"个文件不符合要求<br/>  ";
                   
                }
               
            }
           
            strStatus.Text += strMsg.ToString();
            return true;
        }
        catch (System.Exception error)
        {
            strStatus.Text = error.Message;
            return false;

        }
    }

    protected void Upload_Click(object sender, EventArgs e)
    {
        strStatus.Text = "";//讲提示信息清空
        upMorefile();//调用上传类

    }

public string RandomFileName()
    {//返回随机数的类
        string filename = "";
        string r1 = "";
        string r2 = "";
        string r4 = "";
        Random random = new Random();
        r1 = ((char)random.Next(65, 90)).ToString();//大写字母
        r2 = ((char)random.Next(97, 122)).ToString();//小写字母       
        r4 = random.Next(10000, 999999).ToString();
        filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + r1 + r4 + r2 + r1 + r4 + r1;

        return filename;

    }

前台代码如下

<script language="JavaScript">
            function addFileControl()
            {
                var str = '<INPUT type="file" NAME="File">'
                document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
            }
    </script>
   

            <asp:Panel ID="PanelFileManage" runat="server" Width="100%">
       
        <P id="FileCollection"><INPUT type="file" name="File">&nbsp;</P>
                <p align="center" style="color:Red">
                    允许上传的类型为:ZIP,RAR,TXT,大小为1M以下</p>
            <P align="center"><input onclick="addFileControl()" type="button" value="增加(File)">
                <asp:button id="Upload" Runat="server" Text="上传" Width="56px" OnClick="Upload_Click"></asp:button>
                <input style="WIDTH: 56px; HEIGHT: 24px" onclick="this.form.reset()" type="button" value="重置">
            </P>
            <P align="center"><asp:label id="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
                    Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:label></P>
       
    </asp:Panel>
并且一定要注意在form中添加一个enctype项,才可以使用。如下:

<form id="form1" runat="server" enctype="multipart/form-data">

时间: 2024-07-31 07:20:36

net实现多文件上传代码的相关文章

PHP文件上传代码用法

  php文件上传代码编写过程 1. 先判断是否上传文件 2. 如果有再来判断上传中是否出错 3. 如果出错,则提示出错信息 4. 如查没出错,再判断文件类型 5. 如果类型符合条件,再判断指定目录中有没有存在该文件 6. 如果没有就把该文件移至指定目录 在php中上传文件必须知道的几个东西 $_FILES['myfile']['name'] 是指被上传文件的名称 $_FILES['myfile']['type'] 是指被上传文件的类型 $_FILES['myfile']['size'] 是指被

java 后台实现文件上传代码

问题描述 java 后台实现文件上传代码 java后台做一个文件上传,前端选择了照片点击上传到后台,后台需要保存到服务器上, 这个该怎么做,能不能贴一贴现成的代码?求解 解决方案 try { ??????????? FileItemFactory factory = new DiskFileItemFactory(); ??????????? ServletFileUpload upload = new ServletFileUpload(factory); ??????????? ??????

ajax+php 无刷新文件上传代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.111cn.net/1999/xhtml"> <head> <meta http-equiv="conte

ajax php多文件上传代码

<!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> <meta http-equiv="content-

php简单实用文件上传代码(1/2)

<?php教程 if($_files['file']){  // ----------------------------------------------------------------------------------------------// // // 说明:文件上传   日期:2004-5-2 // // --------------------------------------------------------------------------------------

php 文件上传代码,限制jpg文件

php 文件上传代码,限制jpg文件 <?php     /* 图片上传类 www.lost63.com原创代码 仅限JPG格式图片 */     class uploadFile     {         var $inputName; //input名称         var $fileName; //文件命名         var $fileProperty; //文件属性         var $fileSize=2097152; //文件大小限制,2M         var

php最简单的文件上传代码

php最简单的文件上传代码 <?php    include("top.txt");    include("scripts.txt");    if(isset($_FILES['ufile']['name'])){        echo "Uploading: ".$_FILES['ufile']['name']."<br>";        $tmpName = $_FILES['ufile']['t

php 入门型-文件上传代码

php 入门型-文件上传代码 <?php    if(!empty($_FILES["uploadImage"])) {     // get file name   $filename = basename($_FILES['uploadImage']['name']);      // get extension     $ext = substr($filename, strrpos($filename, '.') + 1);          // check for j

asp.net fileupload文件上传代码

asp教程.net fileupload文件上传代码 <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css教程" href="history/his

asp.net+jquery 文件上传代码(1/2)

asp教程.net+jquery 文件上传代码 <%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml