Delete Files with Folder View and ASP
Question:
How can I delete a file on the server with Folder View and ASP?
Answer:
It can be very easily prepared code for deleting files from the server.
- First, it have to be generate the Delete link.
- If you use the ASP code from the following FAQ for building the Folder View, you just have to change the code a little bit: List server folders and files as tree with Folder View ASP/PHP
- If you do NOT use the mentioned FAQ you have to create the link manually. It should point to the asp file with the Folder View and pass as parameters action="delete" and the file initialized with the full path to the file. To generate the link it could be used the function getDeleteLink and passed as parameter the full path to the file. For example:
- Then add the following code to your page:
- Add the following code before those of the Free View: <% DeleteCheck %>
Just modify the following line from:
Response.Write("<li>" & item.Name & "</li>" & vbCrLf)
To:
Response.Write("<li>" & item.Name & " <a href=""" & getDeleteLink(item.Path) &"""> Delete </a>" &"</li>" & vbCrLf)
Response.Write " <a href=""" & getDeleteLink("C:\Inetpub\wwwroot\Upload\Water_lilies.jpg") &"""> Delete </a>"
<%
Sub DeleteFile(path)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(path) Then
'Delete File
fs.DeleteFile path
End IF
End Sub
Function getDeleteLink(path)
Dim currentPage
currentPage = Request.ServerVariables("SCRIPT_NAME")
'Generate delete link
getDeleteLink = currentPage & "?Action=delete&file=" & Server.Urlencode(path)
End Function
Sub DeleteCheck
'Check delete parameters
If LCase(Request.QueryString("Action")) = "delete" Then
If LCase(Request.QueryString("file")) <> "" Then
'Execute delete
DeleteFile Request.QueryString("file")
End If
End If
End Sub
%>
Note:
1. Make sure that before the call of DeleteCheck there is a code that permit only authorized users to view the page.
Otherwise everyone could delete your files just by passing in the URL Action=delete&file= with the file.
2. Some older clients or proxy implementations limit URI lengths to 255 bytes. Therefore, if you have deep folder structure this could affect the provided code.
DISCLAIMER:
This is extra complimentary content which purpose is to show additional usage that is not part of the product, i.e. it suggests tips for extending the functionality of the product by the users themselves.
It is provided "as is", without warranty of any kind, express or
implied , including but not limited to the warranties of
merchantability, fitness for a particular purpose and nonfringement of
third party rights.
DMXzone does not take responsibility to
support the suggested content to be fully compatible and working as
intended and does not provide support for extended functionality which
is not included in the current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Be the first to write a comment
You must me logged in to write a comment.