这是我第一次翻译文章,不足之处,还请见谅。
印度,现在之所以成为软件出口大国,有很多地方都优于我们。其中一个首要的优势就是印度的官方语言是英语,这就为以英语作为编程语言的印度程序员带来得天独厚的优越条件。对于我们从事it业的人来说,也应该加强自已英语训练。
导言
如果你有个已运行两个月以上时间的网站,你也许会注意到你网站上已积聚了很多的图片文件。尽管我们都已尽力为这些图片文件很好的命名,但当我们去浏览这些图片的文件名时,我们总是会很难想起一些图片文件名有何特殊的含义或用处。
这时,我们通常会重复地用浏览器打开那些图片,看看是什么图片?这时,这段asp代码就可以做为图片浏览器(和清理器)来浏览这些图片并执行清理操作。
编码:
事实上,这个程序就是包含某目录下的所有图片的一个列表页面,用filesystemobject 对象来列举出这些图片文件(gif和jpeg文件).
在页面里加一个链接 toggle display ,来控制是否显示图片。当你有很多文件而不想都加载它们,你可以只让页面的一个图片显示一个链接,相反,如果你不确定一些文件命名是含义,这个功能将能很好的帮助你进行清理操作。
下面是很简洁的代码
<%@ language=vbscript %>
<% option explicit %>
<%
const imagefilepath = "images"
const deletebuttonlabel = "delete selected images"
dim objfso
dim objfolder
dim objfile
dim strfilename
dim strfileextension
dim blnshowimages
if request.querystring("showimages") = "" then
blnshowimages = false
else
blnshowimages = cbool(request.querystring("showimages"))
end if
if request.form("btndelete") = deletebuttonlabel then
set objfso = server.createobject("scripting.filesystemobject")
for each strfilename in request.form("delete")
objfso.deletefile(server.mappath(imagefilepath & "/" & _
strfilename))
next
set objfso = nothing
end if
%>
<html>
<head>
<title>asp 101 image browser & killer!</title>
</head>
<body>
<form action="<%= request.servervariables("url") %>" method="post">
<table border="1">
<tr>
<th>image name</th>
<th>image <a href="<%= request.servervariables("url") %>?
showimages=<%= not blnshowimages %>">(toggle display)</a></th>
<th>delete this image</th>
</tr>
<%
set objfso = server.createobject("scripting.filesystemobject")
set objfolder = objfso.getfolder(server.mappath(imagefilepath))
for each objfile in objfolder.files
strfileextension = lcase(mid(objfile.name, _
instrrev(objfile.name, ".", -1, 1) + 1))
if strfileextension = "gif" or strfileextension = "jpg" or _
strfileextension = "jpeg" then
original image file identification option:
if objfile.type = "gif image" or _
objfile.type = "jpeg image" then
%>
<tr>
<td>
<a href="<%= imagefilepath & "/" & objfile.name %>">
<%= objfile.name %></a>
</td>
<%
if blnshowimages then
%>
<td>
<img src="<%= imagefilepath & "/" & objfile.name %>" />
</td>
<%
else
%>
<td>
<a href="<%= imagefilepath & "/" & objfile.name %>">
view image</a>
</td>
<%
end if
%>
<td align="center">
<input type="checkbox" name="delete"
value="<%= objfile.name %>" />
</td>
<%
end if
next
set objfolder = nothing
set objfso = nothing
%>
<tr>
<td colspan="3" align="right">
<input type="submit" name="btndelete"
value="<%= deletebuttonlabel %>">
</td>
</tr>
</table>
</form>
</body>
</html>
执行删除操作时,一定要注意,如果在程序里不加删除确认提示,程序将不能撤消删除操作。
结论
人们总是不会拿asp来建大型的可升级的网站。(译者注:这句不翻译不是很恰当,根据我的工作经历,asp完全可以建设大型的网站系统。在asp方面中,本人曾参予建设过大型企业内部网,移动行业应用等。)但有时候你可以用这个简化你的工作,即使这代码从没在web上发表。
获取代码
你可以下载包含这个代码的讨论的zip文件http://www.asp101.com/articles/john/imageviewer/imgview.zip。为了控制文件的大小,我没有包含一个图片例子。我再一次提醒你,用这个代码做破坏并删除很多相关图片是很容易的。要注意,没有删除确认时,要确认用图片复本来操作。
代码更新
我已收到很多有关这个代码的email。看了以后,我还是发现有很多问题。
第一:一些人没有意识到他们必须设置代码查找图片的路径。我现在已在放置代码的目录下设一个图片目录.所以,如果我把代码放于http://www.weiw.com的根目录下,代码执行后,将显示http://www.weiw.com/images/下的所有图片。
你可以改变imagefilepath 常量的值。你也可以在这儿放一些虚拟路径,代码将用server.mappath来确定适当的物理路径。你可以很容易地指定你网站上的一些位置。例如,把那个常量的值从"images"改变为"/images",这样可以让程序指向网站根目录下的images目录。同理,可以修改imagefilepath 的值达到同样目的.
第二:当写这个代码时,我简单地用在我机器里的文件类型。这样只能在我的机器上顺利运行。于是发现,文件类型的描述可以改变你的文件联合的基础。为了取得你选择要显示那些基于此类文件代替他们类型的扩展名。我已在这个zip文件中的代码里实现了这个功能.现在这是最新的程序版本。
