向服务器上传一个文件,比如文件路径是:d:\file\1.txt
我按浏览按钮,选择这个文件,在文本框里就会出现 d:\file\1.txt
这时我手动把d:\file\1.txt 改成 d:\\1.txt 这个路径肯定不存在这个文件,
但当我点上传时,依然成功,但服务器上1.txt 这个文件中什么内容都没有,
请问怎么判断上传的文件在本地是否存在?或有什么其他方法解决????
顺便问一下《战神》到底是什么?????

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Button1_Click(object sender, EventArgs e)
      {
        if (FileUpload1.HasFile)
        {
          FileUpload1.SaveAs(Server.MapPath(".") + @"\a.txt");
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:FileUpload ID="FileUpload1" runat="server" />
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
            Text="Button" /></div>
        </form>
    </body>
    </html>
      

  2.   

    也可以<%@ Page Language="C#" AutoEventWireup="true"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Button1_Click(object sender, EventArgs e)
      {
        if (FileUpload1.PostedFile.ContentLength>0)
        {
          FileUpload1.SaveAs(Server.MapPath(".") + @"\a.txt");
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:FileUpload ID="FileUpload1" runat="server" />
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
            Text="Button" /></div>
        </form>
    </body>
    </html>
      

  3.   

    注:以上是在asp.net 2.0环境下的代码
      

  4.   

    string spath=Server.MapPath("../")+@"DataBase\DB\property.dat";
    if(!System.IO.File.Exists(spath))
    {
    Response.Write("<Script>alert('还原的备份文件不存在,请先备份!')</Script>");
    Response.Write("<Script> history.back(); </Script>");
    return;
    }
    ....
                                         try
    {
    DB.MyAdo.Exec(sql);
    Label1.Text="数据库还原成功";
    }
    catch
    {
    Response.Redirect("Error.aspx");
    }
    if(!System.IO.File.Exists(spath)) 这句能判断文件是否存在
      

  5.   

    我也遇过这种问题,如果地址改掉,上传成功后,服务器端会创建一个空文件。
    孟子应当的可以,我用类似的方法。
    也有用JS先判断的,我不会写JS。
      

  6.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    string[]arrExtension = {".gif",".jpg",".jpeg",".bmp",".png",".txt",".doc",".htm",".html",".csf"};
    ArrayList al=new ArrayList();
    HttpFileCollection files = HttpContext.Current.Request.Files;
    try
    {
    for(int i= 0; i< files.Count; i++)
    {
    HttpPostedFile postedFile = files[i];
    string fileName = postedFile.FileName;
    if(fileName.Length!=0)
    al.Add(fileName);
    }
    }
    catch
    {
    throw new Exception();
    }
    for(int i= 0; i< al.Count; i++)
    {
    HttpPostedFile postedFile = files[i];
    if(al[i].ToString().Length>0)
    {
    if(!System.IO.File.Exists(al[i].ToString()))
    {
    Response.Write("<Script>alert('请输入正确的文件路径!')</Script>");
    return;
    }
    else
    {
    if(!Check(System.IO.Path.GetExtension(al[i].ToString()),arrExtension))
    {
    Response.Write("<Script> alert('请选择扩展名为 .gif,.jpg,.jpeg,.bmp,.png,.txt,.doc,.htm,.html 的文件!'); </Script>");
    return;
    }
    else
    {
    string spath=Server.MapPath(".")+@"/Files/"+DateTime.Now.ToString("yyyyMMdd");
    try
    {
    if(!System.IO.Directory.Exists(spath))
    {
    System.IO.Directory.CreateDirectory(spath);
    }
    files[i].SaveAs(spath+@"/"+StrName(al[i].ToString()));
    }
    catch
    {
    Response.Write("<script alert('上传失败!'); </script>");
    }
    }
    }
    }
    else
    {
    Response.Write("<Script> alert('请选择要上传的文件!'); </Script>");
    return;
    }
    }
    Response.Write("<script> alert('上传成功!'); </script>");
    } //判断文件扩展名
    public  bool Check(string str,string [] arr)
    {
    for(int i = 0;i<arr.Length;i++)
    {
    if(str.Equals(arr[i]))
    {
    return true;
    }
    }
    return false;
    } private string StrName(string str)
    {
    string stime=DateTime.Now.ToString("yyyyMMddhhmmssms");
    string []filename=str.Split('\\');
    string file=stime+filename[filename.Length-1];
    return file;
    }
    }
      

  7.   

    【孟子E章】 你给的例子有BUG 当文件大小为0K时,它判断文件为不存在。比如一个空白的TXT文件