向服务器上传一个文件,比如文件路径是:d:\file\1.txt
我按浏览按钮,选择这个文件,在文本框里就会出现 d:\file\1.txt
这时我手动把d:\file\1.txt 改成 d:\\1.txt 这个路径肯定不存在这个文件,
但当我点上传时,依然成功,但服务器上1.txt 这个文件中什么内容都没有,
请问怎么判断上传的文件在本地是否存在?或有什么其他方法解决????
顺便问一下《战神》到底是什么?????
我按浏览按钮,选择这个文件,在文本框里就会出现 d:\file\1.txt
这时我手动把d:\file\1.txt 改成 d:\\1.txt 这个路径肯定不存在这个文件,
但当我点上传时,依然成功,但服务器上1.txt 这个文件中什么内容都没有,
请问怎么判断上传的文件在本地是否存在?或有什么其他方法解决????
顺便问一下《战神》到底是什么?????
解决方案 »
- web.config中configSections节的作用
- 大牛进!跟技术主管发生分歧了,MVC中的Controller是否可以使用继承来写Action??
- 找个案例~~
- asp.net怎样在后台获取confirm提示框的值?然后再执行相应后台代码。
- 在不同的页面使用同一个类实例
- 怎么样判断一个商品当天的点击数
- 怎样才能在后台程序中调用javascript中的字符串变量?
- 读取客户端控件值的问题。
- 我做了个Excel报表程序,客户端启动时调用Excel,现在的问题是客户端不启动Excel,反而在服务器端启动了Excel?
- 用户登录问题
- 关于快速滚动文字的方法
- 怎么在JavaScript中创建2维数组
<!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>
<!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>
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)) 这句能判断文件是否存在
孟子应当的可以,我用类似的方法。
也有用JS先判断的,我不会写JS。
{
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;
}
}