ASP: Document Browser


Introduction

Author: Martin Warning
Year: 2007
License: Free

This code shows an example of how to create a list of documents in a folder using ASP. You can use this to replace the build in IIS document browser and instead show documents within your website template design. It includes a set of icons to use to indicate the file type of a document.

Code

Languages: The following ASP code is a document browser:
<%
' Declare variables
Dim blnDoc, objFSO, objFolder, objItem
Dim strDocName, intDocName, strDocNameAll, j

Function fncFileSize(intFileSize)
' This function converts filesize to GB, MB, KB or B value
	If intFileSize > 1073741824 Then
		fncFileSize = cInt(intFileSize / 1073741824) & " GB"
	ElseIf intFileSize > 1048576 Then
		fncFileSize = cInt(intFileSize / 1048576) & " MB"
	ElseIf intFileSize > 1024 Then
		fncFileSize = cInt(intFileSize / 1014) & " KB"
	Else
		fncFileSize = cInt(intFileSize) & " B"
	End If
End Function

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Check if folder exists
If objFSO.FolderExists(Server.MapPath("docs")) = False Then
' In case folder doesn't exist
		Response.Write("Folder not found")
Else
' In case folder exists
' list all the files in this folder
	Set objFolder = objFSO.GetFolder(Server.MapPath("docs"))

' Loop through all files in folder
	For Each objItem In objFolder.Files
	' When first iteration open list
		If blnDoc <> 1 Then
			Response.Write("<ul style=""list-style-type:none;"">")
		End If
		blnDoc = 1

	' Split docname to get extension
		strDocName = Split(objItem.Name, ".")
		intDocName = uBound(strDocName)

	' Put docname back together but replace _ with a space instead
	' docname is the filename minus the extension of the file
		strDocNameAll = ""
		For j = 0 To intDocName - 1
			strDocNameAll = strDocNameAll & Replace(strDocName(j),"_"," ")
		Next

	' Show an icon of the filetype
	' You will need to have all the icons in the ico folder, code does not check if image exists
		Response.Write("<li><img src=""ico/" & strDocName(intDocName) & ".gif"" alt=""File " & strDocName(intDocName) & """ />")
	' Write the link to the file, including the filesize and modified date
		Response.Write(" <a href=""docs/" & objItem.Name & """>" & strDocNameAll & "</a>")
		Response.Write(" <span class=""filesize"">(" & fncFileSize(objItem.Size) & " - " & objItem.DateLastModified & ")</span></li>")
	Next

' Close off the list
If blnDoc = 1 Then
	Response.Write("</ul>")
Else
' In case no files were found in the folder
	Response.Write("<b>No documents in this folder</b>")
End If

	Set objFolder = Nothing
	Set objItem = Nothing
End If
Set objFSO = Nothing
%>
The following CSS code is included in the <head>:
<style type="text/css">
	img{border-width:0px;border-style:none;}

	.filesize{font-size:x-small;color:#aaaaaa;}
</style>

This code requires to have the documents stored in the folder docs which needs to be located in the folder where the code is stored. It also requires that the file type icons are stored in the ico folder which needs to be located in the folder where the code is stored. These 2 folders can be modified by modifying the ASP code.
The downloadable example is fully functional and includes all components for it to work.

Example: See it in action

Downloads

ASP & Images:
Zip file example.zip


Printed from: http://flyinglowlander.com/ (5/20/2012)
© FlyingLowander.com 2006 - 2012

Visit http://flyinglowlander.com for more XHTML, CSS, ASP and JavaScript examples, templates and tutorials.