Brettski
Brettski

Reputation: 20101

Get a list of files on server with ASP.NET using a picker

Is there a component available list FileUpload which shows files on the server, not the client?

I am basically looking for a clean dialog box to select server side files, like the one used in FileUpload.

Upvotes: 3

Views: 1811

Answers (2)

Jacob T. Nielsen
Jacob T. Nielsen

Reputation: 2978

You cannot browse through the folders of your server in the same way that you would with the FileUpload components, because... well all the files are located on the server and the "clean dialog" that you refer to is client side. You can write you own code to list the files in a dropdown. But if your files are located in multiple folder and you would like to keep some structure, a TreeView might do the trick with something like this:

protected void Page_Load(object sender, EventArgs e)
{
        SetChildFolders(trvFiles.Nodes, @"C:\MyFolder");
}

    private void SetChildFolders(TreeNodeCollection nodes, string path)
    {
        foreach (string directory in Directory.GetDirectories(path))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            TreeNode node = new TreeNode(dirInfo.Name, dirInfo.FullName);

            SetChildFolders(node.ChildNodes, dirInfo.FullName);
            SetChildFiles(node.ChildNodes, dirInfo.FullName);

            trvFiles.Nodes.Add(node);
        }
    }

    private void SetChildFiles(TreeNodeCollection nodes, string path)
    {
        foreach (string file in Directory.GetFiles(path))
        {
            FileInfo fileInfo = new FileInfo(file);
            nodes.Add(new TreeNode(fileInfo.Name, fileInfo.FullName));
        }
    }

You can ofcourse style the treeview in many many ways.

Upvotes: 0

Stephen Wrighton
Stephen Wrighton

Reputation: 37830

Nope. There's not. That said, you can use a listbox, and load the files into it.

public sub file_DatabindListbox(directoryPath as string)
   for each fName as string in io.directory(directorypath).getfilenames()
     dim li as new listitem 
     li.text = io.path.getfilename(fName)
     li.value = fName
     myFileListbox.Items.Add(li)
   next
end sub 

Upvotes: 1

Related Questions