0

How can I get the list of all the uploaded files? I want to list the files in a dropdownlist, so that a user can choose which file to unpack, from the listed dropdown list. I have following code:

var path = Server.MapPath(Project.DataDirectoryPath);
string valueShownInDropDownList = dropDown.selectedValue+".zip"; (The value of uploaded file should be shown here)
var targetToExtract = string.format("{0}/FileBrowser/TestProj/files,projectPath");

using (ZipFile z = ZipFile.Read(targetToExtract.toString()))) 
{
     entry. Extract(targetToExtract);

}
States
  • 141
  • 11

1 Answers1

0

This is how you will get a list of directory:

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
     <asp:ListItem Text="--Select Files--" Value="0" />
</asp:DropDownList>

Server side it will :

 var exDir = @"c:\temp\directory";
    DirectoryInfo dir = new DirectoryInfo(exDir);

    foreach (FileInfo exFile in dir.GetFiles())
    {
           ListItem lst = new ListItem (  xFile.FullName  , "0" );

         DropDownList1.Items.Insert( DropDownList1.Items.Count-1 ,lst);

    }

On Selection of any file , you should post back and download file with the help of Response object.

  Response.ContentType = "application/<type of file>";
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName ));
...
codebased
  • 6,945
  • 9
  • 50
  • 84
  • Your help solved my problem, thanks. Unfortunately I have not enough points to give you a vote... But why must I postback and download with the help of Response object? Is it of security reasons? – States Sep 07 '14 at 15:10
  • You don't have to if you can hit the server without post back. I don't know if you have been using jQuery or any Ajax call methodology. If in case you don't know jQuery, please try using jquery fildownload plugin (http://tinyurl.com/nur5qhg). You would need to add a reference to a jquery and this plugin. Then on click side, when dropdown value change, then call this method from jscript change event of your dropdown: $.fileDownload('/url/to/download.pdf'); Otherwise, you can use httphandler Have a look at this link it will give you an idea:http://tinyurl.com/nkymv27 -Glad I could help you! – codebased Sep 07 '14 at 22:25
  • If you can implement $.ajax call from jquery then that means you are heading to improve your programming skills by implementing responsive development. Thus, you can further improve to get file details from client side and set drop down through Ajax.http://tinyurl.com/lo9l8bc – codebased Sep 07 '14 at 22:27