翻译了一下UDZone.com上一篇介绍如何配合PureASPUpload插件删除上传文件的教程!如果疏漏,敬请指正。
原文地址:
www.udzone.com/ShowDetail.asp?NewsId=1938www.udzone.com/ShowDetail.asp?NewsId=594例子下载:
www.udzone.com/Downloads/Tutorial_deletefilewithdemo.zip/deletefile.zip译文如下:
This tutorial will explain how to delete a file while using an update form or a delete form in conjunction with PureASPUpload 1 & 2.
这个教程将教你在配合使用PureASPUpload 1&2版本的更新或删除表单时,如何删除一个文件。本教程其实是Paul Keur (June 18, 2001) 关于PureASPUpload文件删除教程的一个补充说明!
It's actually very simple.
一般来说非常简单。
First of all you will need to these two functions to make it work.
为了下一步工作,我们首先需要建立两个函数,如下:
'(建一个FileSystemObject对象函数)
<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
%>
'(判断文件是否存在函数)
<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.FileExists(aFileSpec)
End Function
%>
Find your code for either the delete or update sql statement that starts with:
然后从你的网页代码中找到 delete 或 update 的语句片断的位置:
Delete statement:
使用Find菜单搜索如下语句可以找到Delete语句片断位置:
<%
' *** Delete Record: construct a sql delete statement and execute it
Update Statement:
使用Find菜单搜索如下语句可以找到Update语句片断位置:
<%
' *** Update Record: construct a sql delete statement and execute it
Just above these sql statements place the two functions as mentioned above.
在你所搜索到的上面那些SQL语句的地方,把这两个函数的代码加到它们上面去,例如:
----------------------------------------------------------------------------------
<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
%>
<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.FileExists(aFileSpec)
End Function
%>
<%
' *** Delete Record: construct a sql delete statement and execute it
----------------------------------------------------------------------------------
Then insert the code as shown below in the update or delete statement:
然后把下面代码中有颜色的部分插入到updata 或 delete片断的代码中
' execute the delete/update
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
' This is where we delete the file before we delete the record!
' 在我们删除该文件记录在数据库中的信息之前先删除文件!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\upload")
ImagePath = ImagePath & "\" & (rsNews.Fields.Item("image").value)
' check if file exists and if true delete the file
' 判断文件是否已经存在,如为真则删除文件
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
As you see it also will work for an update ! This is very handy when using it for example a news administration with images, where it is allowed to edit/change images. This way all images will be deleted from your system/server.
如你所见,这种方法也可以用到update行为里!当你用它来制作一个带有图片的新闻管理
系统时,使用它会给你减少很多麻烦。这中方法可以删除你的系统/服务器上的所有文件。
One important note is that the database field that is called from the recordset, in this example (rsNews.Fields.Item("image").value), needs to be opened before you call this database field. So, if your recordset is below the sql statement for update or delete, move it to the very top.
有一点需要注意的是,数据库的字段是从recordset调用的,例如在本例中
(rsNews.Fields.Item("image").value), 因此在你调用这个数据库字段之前recordset必须已经建立并且被打开。所以,
如果你的recordset语句片断是在update或delete的SQL语句之后话,必须把它移到尽量靠前。
(简单的理解就是,尽量把recordset语句放在文件的最开头部分)
Example code of the recordset:
例如下面的recordset的代码:
<%
Dim rsNews__MMColParam
rsNews__MMColParam = "1"
if (Request.QueryString("ID") <> "") then rsNews__MMColParam = Request.QueryString("ID")
%>
<%
set rsNews = Server.CreateObject("ADODB.Recordset")
rsNews.ActiveConnection = MM_connUDZONE_STRING
rsNews.Source = "SELECT * FROM tblNews WHERE ID = " + Replace(rsNews__MMColParam, "'", "''") + ""
rsNews.CursorType = 0
rsNews.CursorLocation = 2
rsNews.LockType = 3
rsNews.Open()
rsNews_numRows = 0
%>
That's it !
就是这样!