环境:Win7 + VS2008代码如下:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>Upload</title>
<script type="text/javascript" language="javaScript"> 
function addFile() 

    var str = '<INPUT type="file" size="100" NAME="File">' 
    document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) 
}
</script> 
</head>
<body>
    <form id="form1" method="post" runat="server" enctype="multipart/form-data"> 
    <div style="text-align:center" > 
        <h3>多文件上传</h3> 
        <p id="MyFile"><input type="file" size="100" name="File"/></p> 
        <p> 
        <input type="button" value="增加(Add)" onclick="addFile()"/> 
        <input onclick="this.form.reset()" type="button" value="重置(ReSet)"/> 
        <asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
        </p>
        <p>
        <asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"  
          Width="500px" BorderStyle="None" BorderColor="White"></asp:Label> 
        </p> 
    </div>
    </form>
</body>
</html>
C# 代码如下过长,放在回复中
向没有回家过年的兄弟姐妹们问声过年好,也预祝自己新春大吉。

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text;
    // datetime
    using System.Globalization;public partial class _Default : System.Web.UI.Page 
    {
        struct fileInfo
        {
            public string fileName;
            public Boolean bTxt;
            public Boolean bPdf;
            public Boolean bZip;        public void InitFileInfo()
            {
                fileName = "";
            bTxt = false;
            bPdf = false;
            bZip = false;
            }
        }
        private string[] FileType = { ".pdf", ".zip", ".txt" };  // legal extend file name     
        protected FileStream flog = null;
        protected ArrayList zipArr = new ArrayList();
        protected ArrayList pdfArr = new ArrayList();
        protected ArrayList txtArr = new ArrayList();
        protected ArrayList fileInfoArr = new ArrayList();
        protected ArrayList upFileArr = new ArrayList();    private void Page_Load(object sender, System.EventArgs e) 
        {
            if (this.IsPostBack) this.SaveFiles(); 
        }
        
        private Boolean SaveFiles() 
        {
     
            /*------ Traversing all elements of the file form.------*/
            HttpFileCollection files  = HttpContext.Current.Request.Files;        /*------ Status information ------*/
            StringBuilder strMsg = new System.Text.StringBuilder();        strMsg.Append("上传的文件分别是:<hr color=red>"); 
            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 = fileName;// System.IO.Path.GetExtension(fileName);
                        if (!fileExtension.Contains("."))
                        {
                            Response.Write("该文件类型不允许上传!");
                            return false;
                        }
                        // check file extend name.
                        Boolean bl = CheckFileExtendName(ref fileName);            
                        if (false == bl)
                        {
                            return bl;
                        }
                    }
                }        
                CheckFileIsComplete();
                writeLogFile();            for (int iFile = 0; iFile < files.Count; iFile++) 
                {
                    HttpPostedFile postedFile = files[iFile];                string fileName,fileExtension;                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    Boolean ret = false;
                    // get save path
                    if (0 != upFileArr.Count)
                    {
                        foreach (string strName in upFileArr)
                        {
                            foreach (string strExten in FileType)
                            {                            
                                if (fileExtension == (strName + strExten))
                                {
                                    ret = true;
                                }
                            }                        
                        }
                        if (true == ret)
                        {
                            ret = false;
                            string strPath = "";
                            getSavePath(fileExtension, ref strPath);
                            ///注意:可能要修改你的文件夹的匿名写入权限。 
                            postedFile.SaveAs(strPath + "\\" + fileExtension);
                            strMsg.Append("上传文件的文件名:" + fileExtension + "<br>");
                        }
                    }
                    strStatus.Text = strMsg.ToString(); 
                }
                return true; 
            } 
            catch(System.Exception Ex) 
            { 
                strStatus.Text = Ex.Message; 
                return false; 
            } 
        }
        // Check file extend name and instead the legal file to up-file array list
        private Boolean CheckFileExtendName(ref String strName)
        {           
            int index = strName.LastIndexOf('.');
            char[] c = strName.ToCharArray();
            string strExtend = "";
            bool bl = false;
            string strOnlyName = "";        for (int i = 0; i < strName.Length - index; i++)
            {
                strExtend += c[index + i];
            }        foreach (string str in FileType)
            {
                if (str == strExtend)
                {
                    bl = true;
                    
                    for (int i = 0; i < index; i++)
                    {
                        strOnlyName += c[i];
                    }                if (FileType[0] == strExtend)
                    {
                        pdfArr.Add(strOnlyName);
                    }
                    else if (FileType[1] == strExtend)
                    {
                        zipArr.Add(strOnlyName);
                    }
                    else
                    {
                        txtArr.Add(strOnlyName);
                    }
                }          
            }        if (false == bl)
            {
                Response.Write("该文件类型不允许上传!");           
            }
            return bl;
        }
        // Check file is complete
        // if you have a .zip, a .pdf and a .txt file with the same name, the file is complete.
        private void CheckFileIsComplete()
        {
            int index = 0;
            string tempName;
            fileInfo fileinfo = new fileInfo();
            int i = 0;        for (index = 0; index < zipArr.Count; index++ )
            {
                fileinfo.InitFileInfo();
                tempName = (string)zipArr[index];            fileinfo.fileName = tempName;
                fileinfo.bZip = true;            if ( 0 != pdfArr.Count)
                {
                    for (i = 0; i < pdfArr.Count; i++)
                    {
                        if (tempName == (string)pdfArr[i])
                        {
                            fileinfo.bPdf = true;
                            pdfArr.RemoveAt(i);
                            break;
                        }
                    }
                }            if (0 != txtArr.Count)
                {
                    for (i = 0; i < txtArr.Count; i++)
                    {
                        if (tempName == (string)txtArr[i])
                        {
                            fileinfo.bTxt = true;
                            txtArr.RemoveAt(i);
                            break;
                        }
                    }
                }
                fileInfoArr.Add(fileinfo);
            }        if (0 != pdfArr.Count)
            {
                for (index = 0; index < pdfArr.Count; index++)
                {
                    fileinfo.InitFileInfo();
                    tempName = (string)pdfArr[index];                fileinfo.fileName = tempName;
                    fileinfo.bPdf = true;                if (0 != txtArr.Count)
                    {
                        for (i = 0; i < txtArr.Count; i++)
                        {
                            if (tempName == (string)txtArr[i])
                            {
                                fileinfo.bTxt = true;
                                txtArr.RemoveAt(i);
                                break;
                            }
                        }
                    }
                    fileInfoArr.Add(fileinfo);
                }
            }        if (0 != txtArr.Count)
            {
                for (i = 0; i < txtArr.Count; i++)
                {
                    fileinfo.InitFileInfo();                tempName = (string)txtArr[index];
                    fileinfo.bTxt = true;
                    fileInfoArr.Add(fileinfo);
                }
            }        
        }
        // write all file information to log file.
        private void writeLogFile()
        {
            string strFileName;
            string strLogInfo;
            strFileName = "C:\\LogFile\\";
            strFileName += DateTime.Now.ToString("yyyyMMddhhmmss");
            strFileName += ".log";        Directory.CreateDirectory("LogFile");
            flog = new FileStream(strFileName, FileMode.CreateNew);
                   
            foreach (fileInfo fInfo in fileInfoArr)
            {
                if (fInfo.bTxt && fInfo.bPdf && fInfo.bZip)
                {
                    upFileArr.Add(fInfo.fileName);
                }
                else
                {
                    strLogInfo = DateTime.Now.ToString("hh:mm:ss");
                    strLogInfo += "    ";
                    strLogInfo += fInfo.fileName;                if (!fInfo.bZip)
                    {
                        strLogInfo += " miss zip file.";
                    }
                    if (!fInfo.bPdf)
                    {
                        strLogInfo += " miss pdf file.";
                    }
                    if (!fInfo.bTxt)
                    {
                        strLogInfo += " miss txt file";
                    }
                    strLogInfo += "\n";
                    flog.Position = flog.Length;
                    Encoding encoder = Encoding.UTF8;
                    Byte[] bytes = encoder.GetBytes(strLogInfo);
                    flog.Write(bytes, 0, bytes.Length);
                }            
            }  
            if( 0 != upFileArr.Count )
            {
                strLogInfo = DateTime.Now.ToString("hh:mm:ss");
                strLogInfo += "    ";
                strLogInfo += "Complete files as following:\n";
                foreach(string str in upFileArr)
                {
                    strLogInfo += "            ";
                    strLogInfo += str;
                    strLogInfo += "\n";
                }
                flog.Position = flog.Length;
                Encoding encoder = Encoding.UTF8;
                Byte[] bytes = encoder.GetBytes(strLogInfo);
                flog.Write(bytes, 0, bytes.Length);
            }
            flog.Close();
        } }还是放不下,剩下最好一个函数,只能放在下一个回复中了。
      

  2.   


    private void getSavePath(string fileName, ref string strPath)
        {
            string tempStr = fileName;
            string langStr = "";
            string vehicleStr = "";
            string versionStr = "";
            int index = 0;
            int i = 0;        langStr = tempStr.Substring(tempStr.LastIndexOf("_") + 1, tempStr.LastIndexOf('.') - tempStr.LastIndexOf('_'));                    while(-1 != index)
            {
                index = fileName.IndexOf('_');
                i++;
                if (3 == i)
                {
                    vehicleStr = tempStr.Substring(0, index);
                }
                if (4 == i)
                {
                    versionStr = tempStr.Substring(0, index);
                }
                tempStr = tempStr.Substring(index + 1);
            }
            strPath = "\\";
            strPath += langStr;
            Directory.CreateDirectory(strPath);
            strPath = "\\";
            strPath += vehicleStr;
            Directory.CreateDirectory(strPath);
            strPath = "\\";
            strPath += versionStr;
            Directory.CreateDirectory(strPath);
        }
      

  3.   

    快过年还在上班,兄弟辛苦了~
    是点击上传之后,出现了页面不能显示的问题,我限定了只能上传.txt,.zip及.pdf的上传,而且必须是匹配了之后才能上传,之前报告了一个Logfile不能访问,我把"\LogFile"改成"C:\LogFile",然后就出现点击“上传”就不能显示页面了...
      

  4.   

    代码本身编译没有问题,是运行时产生的问题,时灵时不灵,具体现象是点击“上传”之后,出现了页面不能显示的问题,我限定了只能上传.txt,.zip及.pdf的上传,而且必须是匹配了之后才能上传,(匹配的意思就是必须是同名的文件带有三个不同的后缀名才算一个完整的文件集,例如1.txt,1.pdf,1.zip)之前报告了一个Logfile不能访问,我把"\LogFile"改成"C:\LogFile",然后就出现点击“上传”就不能显示页面了...
      

  5.   

    “C:\LogFile”没有管理员权限是不能写入的吧,会不会是这个问题
      

  6.   

    改下路径,用Server.MapPath试试
    string path=Server.MapPath("LogFile");
      

  7.   

    没效果,代码修改如下:        string strFileName;
            string path=Server.MapPath("LogFile"); //"C:\\LogFile\\";
            strFileName = DateTime.Now.ToString("yyyyMMddhhmmss");
            strFileName += ".log";        Directory.CreateDirectory("LogFile");
            flog = new FileStream(path+ "\\" + strFileName, FileMode.CreateNew);
      

  8.   

    原因应该是在调用Directory.CreateDirectory("LogFile")的时候就已经报错了,web网页中不能调用directory.CreateDirectory来创建文件夹么?
      

  9.   


    我的文件只要小于2M,就可以显示页面,如果超过2M,就会出现不能显示页面,为什么呢?
    web config中设置了HttpRuntime(代码如下):
    <httpRuntime executionTimeout="90" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"
    minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="false"/>
    求指导!
      

  10.   

    C:\LogFile文件夹不能被iis访问。你放在虚拟路径下面就没问题了。
    是权限问题。
      

  11.   

    这个已经解决了,使用System.Web.HttpContext.Current.Request.MapPath之后就好了,为什么是文件超过了2M就显示不了》。..........
      

  12.   

    没有报告异常,我在
    code=csharp] private void Page_Load(object sender, System.EventArgs e) 
        {
            if (this.IsPostBack) this.SaveFiles(); 
        }[code]
    打断点,程序没有进断点,直接报告显示不了。
      

  13.   

    除了web.config文件,其他的代码都贴在帖子里了。方便的话我把工程文件传给兄弟,麻烦兄弟帮我看看到底什么原因,第一次做ASP,还不太懂,谢谢指导。
      

  14.   

    问题解决了,把<httpRuntime executionTimeout="90" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"
    minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="false"/>插入<system.web></system.web>即可。谢谢诸位!