ya , here is improved version of previous post. you can view all files within a particular directory as well as delete any file.
specially this kind of requirements came when i was working in Advertisement Management Module. so there admin can view all advertisement images which he uploaded previously and he can also able to delete from directory. so he can able to manage disk size.
k lets go for coding.
oky so first i need to fetch all images from directory with delete button. i m putting filename in button id value so i can easily recognize that for which file user pressing delete button. and once i’will get filename then rest of coding flow is very simple.. i will pass that filename to delete.jsp file and there i will write logic to delete that file.
listfiles.jsp
JavaScript Function
and bellow screenshot is for listing of files from directory.
and now bellow jsp file show you delete function for that file
<%@ page import="java.io.*" %> <% String id = request.getParameter("id"); File file = new File("C:\\Users\\LYMAN\\Documents\\NetBeansProjects\\WebApplication2\\web\\upload\\"+id+""); if(file.delete()) { out.println("file deleted successfully"); } else { out.println("failed to perform delete operation"); } %>
hope you like this post.. 🙂