Click here to Skip to main content
Click here to Skip to main content
Go to top

Download files from a remote server folder using a Web-service

, 31 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Download Files Using Web-service

Introduction

This is a very basic article which describes how to develop a simple application to download files from a folder in a remote server.

Background

I'm using VS 2010 and a Web service to develop the application.

Using the code

First create a new website in VS 2010 and add a web-service. Then add the following Web methods to your web-service.

[WebMethod()]
public void DownloadToBrowser(string fileName)
{
    FileInfo file = new FileInfo(@"D:\DOCS\"+fileName);
    Context.Response.Clear();
    Context.Response.ClearHeaders();
    Context.Response.ClearContent();
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "text/plain";
    Context.Response.Flush();
    Context.Response.TransmitFile(file.FullName);
    Context.Response.End();
}

[WebMethod()]
public string[] GetFiles()
{
   string[] Files = Directory.GetFiles(@"D:\DOCS\");
   return Files;
}

D:\DOCS\ is the folder where the files reside in the remote server. Now add the Web-Service reference to your website and design your web form markup as follows:

<form id="form1" runat="server">
    <div>
    
        <asp:Button ID="btnsearch" runat="server" Text="Search" 
            onclick="btnsearch_Click" />
        <asp:TextBox ID="txtSearch" runat="server" 
            Height="23px" Width="201px"></asp:TextBox>
        <br />
        <br />
      
        <asp:GridView ID="GridView1" runat="server" 
                       AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Existing Files">
                <ItemTemplate>
                 <asp:LinkButton ID="lbtnDownload" 
                    Text='<%# Eval("FileName")%>' runat="server" 
                    OnCommand ="lbtnDownload_Click" 
                    CommandArgument='<%# Eval("FileName")%>'></asp:LinkButton>
                 </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        
        </asp:GridView>
    </div>
</form>

Now you will see the interface with a Search button and a TextBox. You need to type the name of the file that you need to download in the TextBox and hit Search. Related documents will be added to the GridView. Add the following code to the search button click:

 protected void btnsearch_Click(object sender, EventArgs e)
{
   FileSearchWebService FSW = new FileSearchWebService();
   string[] filePaths = FSW.GetFiles().AsEnumerable().Where(r=>r.Contains(txtSearch.Text)).ToArray();
 
   DataTable DT = new DataTable();
   DataColumn auto = new DataColumn("FileName", typeof(System.String));
   DT.Columns.Add(auto);
   foreach (string item in filePaths)
   {
       DT.Rows.Add(Path.GetFileName(item.ToString()));
   }
    GridView1.DataSource = DT;
    GridView1.DataBind();
}

Now you need to call the web-service with the link button Click event to download the file. Then Run the application and see how it works

protected void lbtnDownload_Click(object sender, CommandEventArgs e)
{
    FileSearchWebService FSW = new FileSearchWebService();
    FSW.DownloadToBrowser(e.CommandArgument.ToString());
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chamara Janaka
Software Developer Insight Software Solutions Lanka PVT Ltd
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
Questionhi Pinmemberbeqiraj2545-Apr-12 22:52 
QuestionOut of memory for Big files PinmemberNeeraj CodeP1-Apr-12 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 1 Apr 2012
Article Copyright 2012 by Chamara Janaka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid