try another color scheme:


Go Back   TECH6.0 > Get Techie > Programming > ASP/ASP.NET


Now Listing Download Files is as easy as 1-2-3

This is a discussion on Now Listing Download Files is as easy as 1-2-3 within the ASP/ASP.NET section, part of the Programming category; This ASP is to list the files under a designated directory ready for download. File names are streamlined in uniform ...

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 11-03-2008, 12:04 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Now Listing Download Files is as easy as 1-2-3

This ASP is to list the files under a designated directory ready for download. File names are streamlined in uniform upper and lower cases, and sorted in an ascending order. Page navigational buttons will avail themselves when there are more than one pages, and will be appropriately updated as and when the user switches between pages, e.g. either "Next" or "Previous" if there are only two pages, additional "First" and/or "Last" if more than two. During browsing, a user can select any file for download by clicking on its name.

Instructions:- Set mDownloadDir and mHttpPath as necessary, save and upload. Thats it.


Code:
<%@ language = VBScript %>

<% 
   Option Explicit
   On Error Resume Next
   Response.buffer = true



   DIM objFSO, objFolder, objFilesInFolder
   DIM arrFiles(), mFileCount, mPageSize, mPageNo, mPageCount
   DIM mDirPath, mFileSpec, mFile, mTemp
   DIM mServerName
   DIM i, j, k, s0, s1
   DIM mformPage, mformCtl, mformCtlVal
   Dim mDownloadDir, mHttpPath

      ' Change as appropriate to your needs
   mPageSize = 10

      ' Change to the actual directory name you have
   mDownloadDir = "downloads"

      ' mDownloadDir should be one subdirectory down from where this page resides, e.g.
      ' if this page is on C:\InetPub\Wwwroot, your dir is C:\InetPub\Wwwroot\Downloads.
      ' The page should of course be in a directory that is defined as the home
      ' or a virtual directory.  
      

   mDirPath = server.mapPath(".") & "\" & mDownloadDir

      '-----------------------------------------------
      ' Following the earlier remarks, if C:\Inetpub\wwwroot is home directory,
       'and this page is in C:\InetPub\Wwwroot, the URL should be
      '-----------------------------------------------
   mHttpPath = "/" & mDownloadDir & "/"

      '-----------------------------------------------
      ' but if this page is not in the home or a virtual directory
     '(e.g,. on PWS, the page may be in C:\InetPub\Webpub but C:\Inetpub\wwwroot is
    'defined as the home directory), then you need to adjust:
      '-----------------------------------------------
   ' mHttpPath = "/WebPub/" & mDownloadDir & "/"  


      ' Instances of object 
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFolder = objFSO.GetFolder(mDirPath)   
   Set objFilesInFolder = objFolder.Files
   mFileCount = objFilesInFolder.Count

   IF mFileCount = 0 Then 
       Response.write "No file available for download yet."
       Set objFSO = Nothing
       Set objFolder = Nothing
       Set objFilesInFolder = Nothing
       Response.End 
   End If

   mPageCount = INT(mFileCount \ mPageSize)
   IF ( mFileCount Mod mPageSize) > 0 THEN
        mPageCount = mPageCount + 1
   End If

     ' Test the existence of a control on form. If exists, its name should
     ' have been wrapped up in HTTP header, since our form uses POST method.
   mformPage = Request.Form("HiddenPage")
   If mformPage <> "" Then 
       IF mPageCount > 1 Then
           mPageNo = Cint(mformPage)
              ' Test the values returned. The unselected ones would have
              ' the pattern of "xxx" and the selected one "xxx, xxx".
           For Each mformCtl In Request.Form
               mformCtlVal = Request.Form(mformCtl)
               IF mformCtlVal = "First, First" Then
                    mPageNo = 1
                    Exit For 
               ElseIf mformCtlVal = "Previous, Previous" Then
                    mPageNo = mPageNo - 1
                    Exit For
               ElseIf mformCtlVal = "Next, Next" Then
                    mPageNo = mPageNo + 1
                    Exit For
               ElseIf mformCtlVal = "Last, Last" Then
                    mPageNo = mPageCount
                    Exit For
               End if
           Next
       Else                            
           mPageNo = 1
       End If
   Else                                ' First run
       mPageNo = 1
   End If

     ' Two dimension array, for File Name and Size columns (entry for Description
     ' column not provided here)
   ReDim arrFiles(mFileCount-1, 1)

     ' Store file name, size info to array 
   i = 0
   For Each mFile In objFilesInFolder 
           ' First char upper, rest lower case, for display purposes only
        mTemp=UCASE(LEFT(mFile.Name,1)) & LCASE((RIGHT(mFile.Name, LEN(mFile.Name) -1)))
        arrFiles(i, 0) = mTemp

           ' Fill file size
        arrFiles(i, 1) = mFile.Size
        i = i + 1
   Next

     ' Sort array by file name
   For i = 0 To UBOUND(arrFiles)-1
        k = i
        s0 = arrFiles(i, 0)
        s1 = arrFiles(i, 1)
        For j = i + 1 To UBound(arrFiles)
            If StrComp(arrFiles(j, 0), s0, vbTextCompare) < 0 Then
                s0 = arrFiles(j, 0)
                s1 = arrFiles(j, 1)
                k = j
            End If
        Next 
        arrFiles(k, 0) = arrFiles(i, 0)
        arrFiles(k, 1) = arrFiles(i, 1)
        arrFiles(i, 0) = s0
        arrFiles(i, 1) = s1
   Next 
%>

<HTML>
<HEAD>
<TITLE> List of files for download </TITLE>
<META http-equiv="Content-Type"; Content="text/html; Charset=iso-8859-1">
</HEAD>
<BODY>
<%'<IMAGE SRC="LisFiles.gif" VSPACE=2 HSPACE=15 ALIGN=LEFT> %>
     
<FONT COLOR="Black" FACE="ARIAL" SIZE="4"> <B> List of Files </B> </FONT> <BR>
<BR>
<FONT FACE="ARIAL" SIZE="1"> <B> &nbsp; Total: &nbsp; <%= cStr(mFileCount) %>
    &nbsp; files. &nbsp; &nbsp; &nbsp; Page &nbsp; <%=Cstr(mPageNo) %> &nbsp; of &nbsp;
    <%=Cstr(mPageCount) %>  &nbsp; page(s). </B></FONT>
<BR> <BR>

<TABLE CELLPADDING=6 CELLSPACING=0 BORDER=1>
<TR> <TH ALIGN=LEFT><B> &nbsp File Name </B></TH>  <TH ALIGN=RIGHT>Bytes</TH></TR>
  

<% 
   k = (mPageNo - 1) * mPageSize
   j = 0
   FOR i = k To mFileCount -1
           '-------------------------------------------------------------------
           ' To form an URL which will be used as an anchor tag for file name
           '-------------------------------------------------------------------
     
        mTemp = mHttpPath & arrFiles(i, 0)
%>

       <TR><TD><A HREF="<%=mTemp%>"> <%=arrFiles(i,0)%> </A> <BR></TD>
         <TD ALIGN=RIGHT> <%=arrFiles(i,1)%> <BR></TD> 


<%      
        j = j + 1                         ' Count to control the max 
        IF j = mPageSize then             ' j is zero-based
             Exit For
        End If
   Next

      ' Fill up one page with blanks
   DO WHILE j < mPageSize
%>
        <TR><TD> &nbsp; <BR></TD>  <TD> &nbsp; <BR></TD> </TR> 
<%
        j = j + 1
   LOOP
%>
</TABLE>

<FORM NAME="UpdPage" ACTION="LisFiles.asp" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="HiddenPage" VALUE="<%=cstr(mPageNo)%>">
<INPUT TYPE="HIDDEN" NAME="OfFirst" VALUE="First" STYLE="POSITION: RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;"> 
<INPUT TYPE="HIDDEN" NAME="OfPrevious" VALUE="Previous" STYLE="POSITION:RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;">
<INPUT TYPE="HIDDEN" NAME="OfNext"  VALUE="Next" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">
<INPUT TYPE="HIDDEN" NAME="OfLast"  VALUE="Last" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">

<% IF mPageCount>1 Then %>
<% If mPageNo=1 Then %>
<INPUT TYPE="SUBMIT" NAME="OfNext"  VALUE="Next" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">
<% If mPageCount>2 Then %>
<INPUT TYPE="SUBMIT" NAME="OfLast"  VALUE="Last" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">
<% End If %>
<% ElseIf mPageNo=mPageCount Then %>
<% If mPageCount>2 Then %>
<INPUT TYPE="SUBMIT" NAME="OfFirst" VALUE="First" STYLE="POSITION: RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;"> 
<% End If %>
<INPUT TYPE="SUBMIT" NAME="OfPrevious" VALUE="Previous" STYLE="POSITION:RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;">
<% ElseIf mPageNo>1 And mPageNo<mPageCount Then %>
<% If mPageCount>2 Then %>
<INPUT TYPE="SUBMIT" NAME="OfFirst" VALUE="First" STYLE="POSITION: RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;"> 
<% End If %>
<INPUT TYPE="SUBMIT" NAME="OfPrevious" VALUE="Previous" STYLE="POSITION:RELATIVE;LEFT=10; WIDTH=70; COLOR=MAROON;">
<INPUT TYPE="SUBMIT" NAME="OfNext"  VALUE="Next" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">
<% If mPageCount>2 Then %>
<INPUT TYPE="SUBMIT" NAME="OfLast"  VALUE="Last" STYLE="POSITION:RELATIVE; LEFT=10; WIDTH=70; COLOR=MAROON;">
<% End If %>
<% End If %>
<% End If %>

</FORM>
</BODY>
</HTML>
<%
   Set objFSO = Nothing
   Set objFolder = Nothing
   Set objFilesInFolder = Nothing
   Response.End
%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #2  
Old 21-03-2008, 06:05 PM
Dreams's Avatar

Moderator
 
Name: Jeetendra Choudhary
Join Date: Feb 2008
Location: India
Phone: Nokia 6300
Posts: 77
Dreams will become famous soon enough
Send a message via AIM to Dreams Send a message via MSN to Dreams Send a message via Yahoo to Dreams
Hello ..!!
Can You Be More Specific On Making Directories And Uploading Which Files To Which Directory Script Is Good I Love It Thanks. Keep It Up.
__________________
Reply With Quote
  #3  
Old 21-03-2008, 06:37 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default

Hello ..!!
Can You Be More Specific On Making Directories And Uploading Which Files To Which Directory Script Is Good I Love It Thanks. Keep It Up.
As Said by Dreams View Post
hiii.... its just soo easy my friend:-

1. Create a directory where you'll store all your download files, for eg. let the directory be named "downloads" and place it onto the root of the server i.e. at c:/inetpub/yoursite.com/wwwroot/downloads

2. Now, your "downloads" dir. should be one subdir. down from where this .ASP page resides, i.e. if your downloads directory is at the above location then this file must be at c:/inetpub/yoursite.com/wwwroot

3. This directory structure should work just perfect with the above script without making any changes.

tell me if still having trouble...
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #4  
Old 21-03-2008, 07:12 PM
Dreams's Avatar

Moderator
 
Name: Jeetendra Choudhary
Join Date: Feb 2008
Location: India
Phone: Nokia 6300
Posts: 77
Dreams will become famous soon enough
Send a message via AIM to Dreams Send a message via MSN to Dreams Send a message via Yahoo to Dreams
Thanks man..!! NOw its working.
can you decorate these page some more.. to make it Interactive and one more thing there no previous and next buttons appeared may be i have only one file on that directory so. But thanks anyway
__________________
Reply With Quote
  #5  
Old 21-03-2008, 07:32 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default

Thanks man..!! NOw its working.
can you decorate these page some more.. to make it Interactive and one more thing there no previous and next buttons appeared may be i have only one file on that directory so. But thanks anyway
As Said by Dreams View Post
Previous and Next buttons appear only when they are required.
See
mPageSize=10 which means you are showing 10 files in a page and if there are 11 or more then, navigation appears.

You may design it according to your requirements. Open the page in Dreamweaver -> Design Mode -> and set colors and layout as desired.
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #6  
Old 21-03-2008, 09:16 PM
Dreams's Avatar

Moderator
 
Name: Jeetendra Choudhary
Join Date: Feb 2008
Location: India
Phone: Nokia 6300
Posts: 77
Dreams will become famous soon enough
Send a message via AIM to Dreams Send a message via MSN to Dreams Send a message via Yahoo to Dreams
Default

Thanks Man ..!!
__________________
Reply With Quote
  #7  
Old 25-03-2010, 05:10 PM
No Avatar

Learner
 
Join Date: Mar 2010
Posts: 9
william is on a distinguished road
Default

Thanks For Sharing This..
Reply With Quote
  #8  
Old 15-04-2010, 08:40 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default

great webby i like this

thanks for sharing
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes




All times are GMT +5.5. The time now is 10:46 AM.

Contact Us - Tech6.com - Link to Us - Advertise - Submit Site - Privacy Statement - TOS - Top