///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字
                HttpPostedFile postedFile = files[iFile];
                string fileName;
                string filePath = System.Web.HttpContext.Current.Request.MapPath("images/");
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                string path = filePath + fileName;
                
                if (fileName != "")
                {
                    if (File.Exists(path))
                    {
                        
                        Response.Write("<Script Language=JavaScript>alert('该文件已经存在!');</Script>");
                    }
                    else
                    {
                     postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
                       
                    }
                }
            }
                   }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;
                   }
各位大侠,帮我看看,代码哪里出问题了,我上传文件的时候,不管有没有重名的文件都会显示
这个语句的内容:Response.Write("<Script Language=JavaScript>alert('该文件已经存在!');</Script>");看了很久都没有看出来,

解决方案 »

  1.   

    File.Exists(path)检查的是Local的文件是否存在, 既然你都要把它上传了, 它在Local当然就是存在的了, 结果就一直都是True啦事实上, 你要检查是应该是Target Location有没有这个File才对, 不应该用File.Exists来判断
      

  2.   

    对啊你的path是本地待上传的文件的路径,你用File.Exists(path)肯定是True
    应该检查你要上传的目的文件夹下有没有这个文件不过看楼主的代码是将images下的文件上传到images下,检查的时候文件肯定是存在的
    另建一个文件夹吧,文件名最好是重新生成
      

  3.   

    path
       你看下这个里面是什么?   string path = filePath + fileName; 
                     
                    if (fileName != "") 
                    { 
                        if (File.Exists(path)) 
                        { 
                             
                            Response.Write(" <Script Language=JavaScript>alert('该文件已经存在!'); </Script>"); 
                        } 
    你这里写的! path =0 路径+ 名字   //问题有可能就出在这里了!你最好自己打断点跟下!
                 if(名字 != “”)
            {
              IF(PATH (这个文件存在))
    {
         弹出 alert 文件存在提示
    }
    }
      

  4.   

    我断点跟踪了,发现我的程序循环了两次,第一次的时候File.Exists(path)是为flase的,第二次就为true,可能是循环出了问题,我试试先