0

Hi how can I show files in my website to download it. I have got code :

Directory.GetFiles("http://example.com/Folder1/Folder2", "*.*")

But it isn't working. I can use it like this :

Directory.GetFiles(@"C:\Program Files\Folder1\Folder2", "*.*")

How can I use this code for show files in http://example.com/Folder1/Folder2 ?

svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    You should take a look at this: http://stackoverflow.com/questions/124492/c-sharp-httpwebrequest-command-to-get-directory-listing – Charlie Jul 27 '12 at 18:56

4 Answers4

2

your question is a little bit vague, but I think I got the thing you're looking for. Anyway I'm going to assume you're using ASP.NET, first step is to create something to display your files. I've used a repeater with only a hyperlink in it like so:

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
    <ItemTemplate>
        <asp:HyperLink ID="hyp" runat="server" />
    </ItemTemplate>
</asp:Repeater>

After this you'll need to fill up the repeater. You can do that in the pageload like this:

if (!Page.IsPostBack)
{
    string[] files = Directory.GetFiles(@"C:\testfolder");
    rpt.DataSource = files;
    rpt.DataBind();
}

Next step you can do is complete the ItemDataBound method like so:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        string file = e.Item.DataItem as string;
        HyperLink hyp = e.Item.FindControl("hyp") as HyperLink;
        hyp.Text = file;
        hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);
    }
}

As you can see in the navigate url, we're going to use an HttpHandler. When you create a new Handler file (.ashx). In its ProcessRequest method you will need something like this, so the file is avaible for downloading:

public void ProcessRequest(HttpContext context)
{
    context.Response.Clear();
    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Request.QueryString["file"]);
    context.Response.WriteFile(context.Request.QueryString["file"]);
    context.Response.End();
}

Don't forget to register your handler in the web.config in the system.web node like so:

<httpHandlers>
  <add verb="*" path="~/Handlers/FileHandler.ashx?file={0}" type="StackOverflow.Questions.Handlers.FileHandler, StackOverflow.Questions"/>
</httpHandlers>

Please keep in mind that passing paths through the query string like I did shouldn't be done, but I don't know how your application is working so find something that suits you.

Good luck!

1

There is no way to do that on a decent web site, you can download a file if you know it's URL, but there is no way to get all files in direcotry or get directory structure if web site is properly set.

To download file from web server you have to use WebClient, something like this :

WebClient wc = new WebClient();
wc.DownloadFile(" http://example.com/Folder1/Folder2/File.txt", "C:\\temp\\File.txt");

To download file from FTP server, use FtpWebRequest , here is the example to list directory files :

http://www.coding.defenselife.com/index.php/articles/20-ftpwebrequest-sample-c

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
0

From the docs: *Returns the names of files (including their paths) in the specified directory.

You are passing it a URL to a web site, so that is why it's not returning anything - it is expecting a local path.

Brian
  • 6,910
  • 8
  • 44
  • 82
0

Thanks everyone for theirs answers. I downloaded ftp client for C#. And I used that code :

            ftp.Connect("ftp.domain.com");
            ftp.Login("user", "pw");

            // If files in : domains/httpdocs/Install/Program
            ftp.ChangeFolder("domains");
            ftp.ChangeFolder("httpdocs");
            ftp.ChangeFolder("Install");

            ftp.DownloadFiles("Program",
            "C:/Program Files/Install/", new RemoteSearchOptions("*.*", true));

            ftp.Close();

You can download ftp client from here: http://www.limilabs.com/ftp