How do you get a list of files from server folder to display on in a list control

M

maglev_now

I'm using .net 1.1 trying to get a list of files in folder on the server.
The user would select the file they want to download from a DropDownList.
Can someone tell me how this should be done? I can't find the right class
or object to use. Is it the File class or the FolderBrowser? Stuck like
Chuck!!! Any suggestions welcome. Thanks.
 
S

Shahzad Godil

You can use FileSystemObject like this.

<%
Set objFSO=Server.CreateObject("Scripting.filesystemobject")
Set objFold=objFSO.GetFolder("E:\admin\uploads")
Set objfiles=objFold.files
%>
and then generating my drop down using

<select name="productThumb" id="productThumb">
<option><%For Each objFile in objFolder.files
objFile.Name
Next
%></optio

Shahzad Godil

Karachi-Pakistan.
 
L

Laurent Bugnion

Hi,

Shahzad said:
You can use FileSystemObject like this.

<%
Set objFSO=Server.CreateObject("Scripting.filesystemobject")
Set objFold=objFSO.GetFolder("E:\admin\uploads")
Set objfiles=objFold.files
%>
and then generating my drop down using

<select name="productThumb" id="productThumb">
<option><%For Each objFile in objFolder.files
objFile.Name
Next
%></optio

Shahzad Godil

Karachi-Pakistan.

Are you seriously proposing using the FileSystemObject in an ASP.NET
environment? That's sub-optimal to say the least

To the OP: You get a list of files in a folder using the
System.IO.DirectoryInfo class.

For example:

DirectoryInfo dir = new DirectoryInfo( "c:\\temp" );
if ( dir.Exists )
{
FileInfo[] filesInDir = dir.GetFiles();
}

http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx

HTH,
Laurent
 
M

maglev_now

Thanks for the responses...

This is what I got using a DropDownList and a Button.

Dim parentfolder As String = "recipes"
Dim myDir As New DirectoryInfo(Server.MapPath(parentfolder))
Me.DDLFileNames.DataSource = myDir.GetFiles("*.doc")
Me.DDLFileNames.DataBind()
Session("FilePath") = myDir.ToString

**Click the download button and...
Session("FileName") = Me.DDLFileNames.SelectedItem.ToString
Response.Redirect("FileDownload.aspx")

**page load in FileDownload.aspx
Dim filename As String = DirectCast(Session("FileName"), String)
Dim filepath As String = DirectCast(Session("FilePath"), String)
Dim fullpath As String = filepath + "\" + filename
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & filename & """")
Response.Flush()
Response.WriteFile(fullpath)
Session.Abandon()

This works great running at home but when I upload it to my server the
file(aword.doc) gets downloaded and it is empty. Any ideas why that is?


Thanks again.


Laurent Bugnion said:
Hi,

Shahzad said:
You can use FileSystemObject like this.

<%
Set objFSO=Server.CreateObject("Scripting.filesystemobject")
Set objFold=objFSO.GetFolder("E:\admin\uploads")
Set objfiles=objFold.files
%>
and then generating my drop down using

<select name="productThumb" id="productThumb">
<option><%For Each objFile in objFolder.files
objFile.Name
Next
%></optio

Shahzad Godil

Karachi-Pakistan.

Are you seriously proposing using the FileSystemObject in an ASP.NET
environment? That's sub-optimal to say the least

To the OP: You get a list of files in a folder using the
System.IO.DirectoryInfo class.

For example:

DirectoryInfo dir = new DirectoryInfo( "c:\\temp" );
if ( dir.Exists )
{
FileInfo[] filesInDir = dir.GetFiles();
}

http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx

HTH,
Laurent




--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
M

maglev_now

Problem solved... Thanks for the help.
Thanks for the responses...

This is what I got using a DropDownList and a Button.

Dim parentfolder As String = "recipes"
Dim myDir As New DirectoryInfo(Server.MapPath(parentfolder))
Me.DDLFileNames.DataSource = myDir.GetFiles("*.doc")
Me.DDLFileNames.DataBind()
Session("FilePath") = myDir.ToString

**Click the download button and...
Session("FileName") = Me.DDLFileNames.SelectedItem.ToString
Response.Redirect("FileDownload.aspx")

**page load in FileDownload.aspx
Dim filename As String = DirectCast(Session("FileName"), String)
Dim filepath As String = DirectCast(Session("FilePath"), String)
Dim fullpath As String = filepath + "\" + filename
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & filename & """")
Response.Flush()
Response.WriteFile(fullpath)
Session.Abandon()

This works great running at home but when I upload it to my server the
file(aword.doc) gets downloaded and it is empty. Any ideas why that is?


Thanks again.


Laurent Bugnion said:
Hi,

Shahzad said:
You can use FileSystemObject like this.

<%
Set objFSO=Server.CreateObject("Scripting.filesystemobject")
Set objFold=objFSO.GetFolder("E:\admin\uploads")
Set objfiles=objFold.files
%>
and then generating my drop down using

<select name="productThumb" id="productThumb">
<option><%For Each objFile in objFolder.files
objFile.Name
Next
%></optio

Shahzad Godil

Karachi-Pakistan.

Are you seriously proposing using the FileSystemObject in an ASP.NET
environment? That's sub-optimal to say the least

To the OP: You get a list of files in a folder using the
System.IO.DirectoryInfo class.

For example:

DirectoryInfo dir = new DirectoryInfo( "c:\\temp" );
if ( dir.Exists )
{
FileInfo[] filesInDir = dir.GetFiles();
}

http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx

HTH,
Laurent

I'm using .net 1.1 trying to get a list of files in folder on the
server. The user would select the file they want to download from a
DropDownList. Can someone tell me how this should be done? I can't
find the right class or object to use. Is it the File class or the
FolderBrowser? Stuck like Chuck!!! Any suggestions welcome. Thanks.



--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top