///'遍历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>");看了很久都没有看出来,
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>");看了很久都没有看出来,
应该检查你要上传的目的文件夹下有没有这个文件不过看楼主的代码是将images下的文件上传到images下,检查的时候文件肯定是存在的
另建一个文件夹吧,文件名最好是重新生成
你看下这个里面是什么? string path = filePath + fileName;
if (fileName != "")
{
if (File.Exists(path))
{
Response.Write(" <Script Language=JavaScript>alert('该文件已经存在!'); </Script>");
}
你这里写的! path =0 路径+ 名字 //问题有可能就出在这里了!你最好自己打断点跟下!
if(名字 != “”)
{
IF(PATH (这个文件存在))
{
弹出 alert 文件存在提示
}
}