用c#写了个文件上传类,同时上传多个文件时,只有一个上传成功,     public class UploadFile
     {
           private string filePath = null;
           private string fileType = null;
           private int fileSize = 0;
           /// 初始化变量
           public UploadFile()
           {
                 filePath = "upload_img/";            //上传路径
                 fileType = "jpg|gif|bmp";      //允许上传的文件类型
                 fileSize = 200;                        //允许上传的文件大小
           }           public string FilePath
           {
                 set { filePath = value; }
                 get { return filePath; }
           }           public int FileSize
           {
                 set { fileSize = value * 1024; }
                 get { return fileSize; }
           }           public string FileType
           {
                 set { fileType = value; }
                 get { return fileType;}
           }           /// 上传图片
           /// 上传控件名称
           /// 是否以当前时间创建文件夹,1为创建,2为指定
           /// 返回上传图片的文件名
           public string Upload(System.Web.UI.HtmlControls.HtmlInputFile InputFileName,int DirectoryFlag)
           {
                 try
                 {
                       //获得客户端文件的路径
                       string SourceFile = InputFileName.Value.Trim();
                       if ((SourceFile == string.Empty) || (SourceFile == null))
                       {
                             return null;
                       }
                       //获得文件扩展名
                       //string upFileType = SourceFile.Substring(SourceFile.LastIndexOf(".")+1);
                       string upFileName  = InputFileName.PostedFile.FileName;
                       string upFileType = System.IO.Path.GetExtension(upFileName).ToLower();
                       string upFileType2 = upFileType.Substring(1,upFileType.Length-1);      //去除扩展名中的点
                       //获得上传文件的大小
                       long upfileSize = InputFileName.PostedFile.ContentLength;
                       //检测上传文件格式
                       string[] arrFileType = fileType.Split('|');
                       bool allowFlag = false;
                       foreach(string fileTypeItem in arrFileType)
                       {
                             if(fileTypeItem == upFileType2)
                             {
                                   allowFlag = true ;
                                   break;
                             }
                       }
                       if(!allowFlag)
                       {
                             message("文件格式不正确,目前支持的格式为:"+fileType);
                             return null;
                       }
                       //判断上传文件大小
                       if(upfileSize > fileSize)
                       {     
                             message("上传的图片不能大于" + (fileSize/1024) + "KB");
                             return null;
                       }                       //以当前时间保存图片的名字或创建文件夹的名字
                       string RandomFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + GetRandom(4);
                       //获得站点的物理路径
                       string uploadFilePath = null;
                       //如果为true则以当前时间创建文件夹,否则为设置的文件夹
                       if (DirectoryFlag == 1)
                       {
                             uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + DateTime.Now.ToString("yyyyMMdd") + "/";
                       }
                       else
                       {
                             uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + filePath ;
                       }
                       System.IO.DirectoryInfo upDirectory = new System.IO.DirectoryInfo(uploadFilePath);
                       //判断文件夹是否存在,不存在则创建
                       if(!upDirectory.Exists)
                       {
                             upDirectory.Create();
                       }                       string fileName = RandomFileName + upFileType;
                       filePath = uploadFilePath + fileName;
                       InputFileName.PostedFile.SaveAs(filePath);                       return fileName;
                 }
                 catch
                 {
                       //异常
                       message("系统错误,上传失败!");
                       return null;
                 }
           }           private string GetRandom(int NumberLen)
           {
                 //Create a new Random class
                 Random myRandom = new Random();
                 string myRandomNumber = string.Empty;
                 for (int i=0;i<NumberLen;i++)
                 {
                       int RandomNumber = myRandom.Next(9);
                       myRandomNumber += RandomNumber;
                 }
            return myRandomNumber;
           }           private void message(string msg)
           {
                 System.Web.HttpContext.Current.Response.Write(msg);
           }
     }例子:
<INPUT type="file" id="file1" runat="server"><br>
<INPUT type="file" id="file2" runat="server" NAME="file2"><br>UploadFile myUpload = new UploadFile();
myUpload.FileSize = 200;
//myUpload.FilePath = "test/";
myUpload.FileType = "jpg|gif";
string strFile = myUpload.Upload(file1,0);
string strFile2 = myUpload.Upload(file2,0);

解决方案 »

  1.   

    还是解决不了,上传第二个文件的时候,变量会++的,
    如果是上传到upload目录,就会变成:upload/upload/,上传也不成功.
      

  2.   

    http://truly.cnblogs.com/archive/2006/04/06/368524.html
      

  3.   

    foreach (string fileTypeItem in arrFileType)
                        {
                            if (fileTypeItem == upFileType2)
                            {
                                allowFlag = true;
                                break;
                            }
                        }是不是这里的break;出错啊。
    你看看这里
      

  4.   

    不是,主要是上传第二个文件的时候,Upload方法里面的变量就累加了.
      

  5.   

    uploadFilePath = "C:\\Temp\\WebSite1/C:\\Temp\\WebSite1/upload_img/200604251257513235.jpg"已经找到错了,
    错在这里
            if (DirectoryFlag == 1)
                        {
                            uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + DateTime.Now.ToString("yyyyMMdd") + "/";
                        }
                        else
                        {
                            uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + filePath;
                        }
    第二次时放文件的路径有变动了。所以不能新建,你应该把System.Web.HttpContext.Current.Server.MapPath(".") 定义成一个常量,
    所以这里的路径错了。你查一下这个
      

  6.   

    问题出在哪里?在下面这一句
    filePath = uploadFilePath + fileName;
    由于你的两次调用,并没有重新对类进行实例化,而上述的filePath被你重复引用,又当作一个指引路径,最后又被你变成实际绝对路径,所以,在第二次上传的时候,就产生了路径重叠现象.
    以下是简单的修改代码public class UploadFile
    {
    private string FilePath_m = "upload_img/";
    private string fileType = null;
    private int fileSize = 0;
    /// 初始化变量
    public UploadFile()
    {
    //filePath = "upload_img/";            //上传路径
    fileType = "jpg|gif|bmp";      //允许上传的文件类型
    fileSize = 200;                        //允许上传的文件大小
    } public string FilePath
    {
    set { FilePath_m = value; }
    get { return FilePath_m; }
    } public int FileSize
    {
    set { fileSize = value * 1024; }
    get { return fileSize; }
    } public string FileType
    {
    set { fileType = value; }
    get { return fileType;}
    } private string uploadFilePath_m;
    public string uploadFilePath
    {
    set { uploadFilePath_m = value; }
    get { return uploadFilePath_m;}
    } /// 上传图片
    /// 上传控件名称
    /// 是否以当前时间创建文件夹,1为创建,2为指定
    /// 返回上传图片的文件名
    public string Upload(System.Web.UI.HtmlControls.HtmlInputFile InputFileName,int DirectoryFlag)
    {
    try
    {
    //获得客户端文件的路径
    string SourceFile = InputFileName.Value.Trim();
    if ((SourceFile == string.Empty) || (SourceFile == null))
    {
    return null;
    }
    //获得文件扩展名
    //string upFileType = SourceFile.Substring(SourceFile.LastIndexOf(".")+1);
    string upFileName  = InputFileName.PostedFile.FileName;
    string upFileType = System.IO.Path.GetExtension(upFileName).ToLower();
    string upFileType2 = upFileType.Substring(1,upFileType.Length-1);      //去除扩展名中的点
    //获得上传文件的大小
    long upfileSize = InputFileName.PostedFile.ContentLength;
    //检测上传文件格式
    string[] arrFileType = fileType.Split('|');
    bool allowFlag = false;
    foreach(string fileTypeItem in arrFileType)
    {
    if(fileTypeItem == upFileType2)
    {
    allowFlag = true ;
    break;
    }
    }
    if(!allowFlag)
    {
    message("文件格式不正确,目前支持的格式为:"+fileType);
    return null;
    }
    //判断上传文件大小
    if(upfileSize > fileSize)
    {     
    message("上传的图片不能大于" + (fileSize/1024) + "KB");
    return null;
    } //以当前时间保存图片的名字或创建文件夹的名字
    string RandomFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + GetRandom(4); //获得站点的物理路径
    //如果为true则以当前时间创建文件夹,否则为设置的文件夹
    if (DirectoryFlag == 1)
    {
    this.uploadFilePath_m = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + DateTime.Now.ToString("yyyyMMdd") + "/";
    }
    else
    {
    this.uploadFilePath_m = System.Web.HttpContext.Current.Server.MapPath(".") + "/" + this.FilePath ;
    } System.IO.DirectoryInfo upDirectory = new System.IO.DirectoryInfo(uploadFilePath); //判断文件夹是否存在,不存在则创建
    if(!upDirectory.Exists)
    {
    upDirectory.Create();
    } string fileName = RandomFileName + upFileType;
    //this.FilePath = this.uploadFilePath + fileName;
    InputFileName.PostedFile.SaveAs(this.uploadFilePath + fileName); return fileName;
    }
    catch
    {
    //异常
    message("系统错误,上传失败!");
    return null;
    }
    } private string GetRandom(int NumberLen)
    {
    //Create a new Random class
    Random myRandom = new Random();
    string myRandomNumber = string.Empty;
    for (int i=0;i<NumberLen;i++)
    {
    int RandomNumber = myRandom.Next(9);
    myRandomNumber += RandomNumber;
    }
    return myRandomNumber;
    } private void message(string msg)
    {
    System.Web.HttpContext.Current.Response.Write(msg);
    }
    }
      

  7.   

    找出真正的问题所在了。
    你看这句,
        filePath = uploadFilePath + fileName;你走了之后没有把filePath清掉,所以就重复了。