欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

请问下asp高手可以检测文件是否存在并进行判断吗?

shiping1 的头像

 

2013-09-30 20:44lunten | 浏览 267 次
比如一段代码
<img src=img/111.jpg>
可以做到如果img文件夹下没有111.jpg的话就不显示这段代码或是直接选择另外一张图片
不知道ASP能否做到这点

正确答案
<img src="img/111.jpg" onerror="this.src='另一张确实存在的图片名';" /> 以上是用Javascript实现的,是在浏览器端实现的,不占用服务器资源。 如果要通过ASP在服务器端实现,则这样: 
<% 
set fso=server.createobject("scripting.filesystemobject")
 if fso.fileexists(server.mappath("img/111.jpg")) 
then response.write "<img src=img/111.jpg>"
 else response.write "<img src=另外一张图片名>"
 end if set fso=nothing 
%> 

正确答案2
强烈推荐第一种方法
来自 http://zhidao.baidu.com/question/390124020992620845.html



这段代码加在保存之前
<% 
'ASP判断文件是否存在以及删除文件实例代码
dim htmlFilefs
htmlFile="../book_show.html"
htmlFile=server.MapPath(htmlFile)                        
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If fs.FileExists(htmlFile) Then   '判断文件是否存在
         fs.DeleteFile htmlFile,true  '如果文件存在,则删除文件
end if
Set fs=Nothing
%>
来自 http://zhidao.baidu.com/question/413428959.html

普通分类: