Tuesday, March 27, 2012

List of files from a folder to create dynamic links on a page

i have a web page that i would like to have the links for it load dynamically.

meaning that the user clicks on a link and it take them to a page for a particular vendor on that vendors page in the body of the page I would like to have links created for each of the files that are in that vendors folder. I want it like this so that if a person uploads a new file that folder (which is a daily occurance) a link will be created to that file the the link being the name of the file. What is the easiest/best way to do this?

thanks in advance

Have a look at the FileInfo Classhttp://msdn2.microsoft.com/en-us/library/system.io.fileinfo(vs.80).aspx


yes i have seen that information before, but it is for pulling individual file attributes which i can do. what i am looking for is for it to pull for each individual file in a folder and then placing the name of the file as a link on the page. I have not seen how this is done in link


I'm not sure of the exact syntax but have you tried something like:

Dim filInfoAs FileInfo#
ForEach filAsStringIn Directory.GetFiles("Path")
filInfo =New FileInfo(fil)
Response.Write("<a href="http://links.10026.com/?link="" & filInfo.Name &""">" & filInfo.Name &"</a>")
Next


I am an uber newbie :( . I am just wondering how i throw that in a page. i need to have it displayed with in a div

do i have to put to some kind of <script/> tag sorry to be a pain in the but...never used ASP.NET before and just need to get something up simple until i can learn the ins and outs...have a deadline on a project that i got pulled in to

<div id="three-column-container">
<div id="three-column-left">
<h2>Company Name Here</h2>
Dim filInfo As FileInfo#
For Each fil As String In Directory.GetFiles("company/files")
filInfo = New FileInfo(fil)
Response.Write("<a href="http://links.10026.com/?link="" & filInfo.Name & """>" & filInfo.Name & "</a>")
Next

</div>


Create a new page and add this code to the page behind:

Imports System.io
Imports System.Collections.Generic

PartialClass Default
Inherits System.Web.UI.Page

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

Dim filInfoAs FileInfo
Dim listOfFilesAsNew List(Of FileInfo)

ForEach filAsStringIn Directory.GetFiles(Server.MapPath("~\MyFolder"))

filInfo =New FileInfo(fil)
listOfFiles.Add(filInfo)

Next

Me.rptFiles.DataSource = listOfFiles
Me.rptFiles.DataBind()

EndSub

EndClass

then add this to the page HTML source:

<%@.PageLanguage="VB"AutoEventWireup="false"CodeFile="Default.aspx.vb"Inherits="Default" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:RepeaterID="rptFiles"runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><ahref="MyFolder/<%# Eval("Name") %>"><%# Eval("Name") %></a></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

I have a folder called MyFolder with some text files in it and the code above displays thems with links. See if you can get the above working then build from their. Let me know how you get on.

0 comments:

Post a Comment