You would use the System.IO namespace for something like this. (I am going to show this in C#). At the top of the page, make sure the namespace is included:
using System.IO.
In the web page where you want to display the files you will need to code the Page_Load event something like this:
if (!this.IsPostBack)
{
// specify the documents folder in a variable.
string dirPath = "C:\...";
// get the files into a string array.
string[] files = Directory.GetFiles(dirPath
// iterate through the files, adding each file name to a list box.
foreach (string file in files)
{
// add to list box.
lstBox.Items.Add(Path.GetF
}
}
This will show the filename only in the list box, but keep the whole path in the value attribute of each option. That will be important when someone selects a file.
You should have a command button on the page that the user clicks to download the selected file. In that button's click event, do something like this:
if (this.lstBox.SelectedItem != null)
{
// the Value property of the selected item should have the full path.
FileStream fs = File.OpenRead(this.lstBox.
BinaryReader br = new BinaryReader(fs);
byte[] fileBytes = br.GetBytes(fs, 0, Convert.ToInt32(fs.Length)
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream"
Response.BinaryWrite(fileB
Response.Flush();
Response.Close();
}
Two things about this: Showing your servers file system in the option tags is generally not considered good security practice. Take that into account. Also, while you could allow the user to select multiple documents in your list box, you really cannot stream multiple docs to the user's machine all at the same time, so you'll want to stick to one at a time.
John
Main Topics
Browse All Topics





by: daniel_ballaPosted on 2006-10-27 at 14:50:59ID: 17822659
Hi SWRO,
You cannot use an input file control to choose the documents on your server but instead you can populate a ListBox control with the selected documents (or any other control you fency). As far as downloading them to the user PC, it depends on what type of documents they are. If the file type is not associated to something on the user's browser it will by default prompt to save it if you link to the document. You might need to set up IIS to allow the certain extension but that is a minor inconvenient.
Hope this helps
Cheers!
Dani