Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying contents of a folder with ASP and allow users to download

Status
Not open for further replies.

isthisthingon

IS-IT--Management
May 17, 2005
65
US
I have an asp page called protected.asp which is accessed after users login. I would like users to be able to click a button and view the contents of a folder (called "uploads") and download the files (mainly pdf's)they wish to view. How do I do this? All help is appreciated!!! Please go easy, I am an ASP newbie.

The next step to this would be to allow users to upload files with a filename and description for users to view.

Thanks!!!!!
 
Take a look at W3Schools tutorials about FSO (File System Object), it's everything you need.
 
Here's what I came up with but when I click on the link to the file in the table, it says page not found. Any ideas?!


<<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">

Sub Page_Load(sender as Object, e as EventArgs)
Dim strPath As String = "./uploads/"
Dim myDirInfo As DirectoryInfo

lblPath.Text = strPath

myDirInfo = New DirectoryInfo(Server.MapPath(strPath))

dgFileList.DataSource = myDirInfo.GetFiles()
dgFileList.DataBind()
End Sub

</script>


<html>
<head>
<title>ASP.NET Directory List Sample</title>
</head>
<body>

<p>
Contents of <strong><asp:Literal id="lblPath" runat="server" /></strong>
</p>

<asp:DataGrid id="dgFileList" runat="server"

Border = 5
BorderColor = "green"
CellSpacing = 0
CellPadding = 2
HeaderStyle-BackColor = "#006600"
HeaderStyle-ForeColor = "#FFFFFF"
HeaderStyle-Font-Bold = "True"
ItemStyle-BackColor = "#CCFFCC"

AutoGenerateColumns="False"
>

<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="dir/{0}" DataTextField="Name" HeaderText="File Name:" />
<asp:BoundColumn DataField="Length" HeaderText="File Size (bytes):" ItemStyle-HorizontalAlign="Right" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created:" />
<asp:BoundColumn DataField="Extension" HeaderText="File Type:" />
</Columns>
</asp:DataGrid>

<hr />

<p>
Click <a href=" to log out.
</p>

</body>
</html>
 
Oh, sorry, when you told only ASP, I thought that you was telling about ASP3...

Can you show the HTML output generated when the is ran?
 
The page cannot be found
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

--------------------------------------------------------------------------------

Please try the following:

If you typed the page address in the Address bar, make sure that it is spelled correctly.

Open the home page, and then look for links to the information you want.
Click the Back button to try another link.
HTTP 404 - File not found
Internet Information Services


--------------------------------------------------------------------------------

Technical Information (for support personnel)

More information:
Microsoft Support
 
Before clicking on the link, right click on the page and open the HTML source code, and post it here.
 
before clicking on the link to open the file or to open the aspx page itself?
 
This is the source code using the method you described of the aspx page itself which displays the contents of the directory and allows users to click on the file.
 
sorry, forgot to paste

<


<html>
<head>
<title>ASP.NET Directory List Sample</title>
</head>
<body>

<p>
Contents of <strong>./uploads/</strong>
</p>

<table cellspacing="0" cellpadding="2" rules="all" Border="5" bordercolor="Green" border="1" id="dgFileList" style="border-color:Green;border-collapse:collapse;">
<tr style="color:White;background-color:#006600;font-weight:bold;">
<td>File Name:</td><td>File Size (bytes):</td><td>Date Created:</td><td>File Type:</td>
</tr><tr style="background-color:#CCFFCC;">
<td><a href="dir/web.config">web.config</a></td><td align="Right">309</td><td>2/23/2006 11:59:40 AM</td><td>.config</td>
</tr>
</table>

<hr />

<p>
Click <a href=" to log out.
</p>

</body>
</html>
 
there is no web.config file on the web server at all .. .. what do I do next? This is puzzling since I have asp pages that are working fine on the site.
 
For the record, even if there was a web.config file, IIS wouldn't let you have access to it, or request it from a browser.

Split the following line into two lines:
Code:
myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
and slap a breakpoint on it. Basically assign the returned value from Server.MapPath to a variable then use that variable for your DirectoryInfo object. This way you can see what path it builds before it continues.

The "dir" addition is coming from the property here:
Code:
DataNavigateUrlFormatString="dir/{0}"

i'm fairly certain that needs to be corrected if I have read your intentions and posts correctly.

-T

barcode_1.gif
 
thanks for your help, pardon my newbie status. how would i break that down to two lines? thanks!!!
 
Code:
Dim strCompletePath As String
strCompletePath = Server.MapPath(strPath)
myDirInfo = New DirectoryInfo(strCompletePath)

Or just...

Code:
strPath = Server.MapPath(strPath)
myDirInfo = New DirectoryInfo(strPath)
 
Thanks for your help. I have replaced that line with the one you have posted 24 feb 06 10:47. It still returns the page cannot be displayed error once I click on one of the files that are displayed. Any ieas?
 
Thanks for your help. I have replaced that line with the one you have posted 24 feb 06 10:47. It still returns the page cannot be displayed error once I click on one of the files that are displayed. Any ideas? The goal here is to display the list of files in a directory, which it does. And then allow users to click on a file and download it which is where it displays page cannot be displayed once the file is clicked on.
 
i made the change you suggested and also deleted dir from the following: DataNavigateUrlFormatString="dir/{0}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top