Click here to Skip to main content
15,892,965 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
31 Mar 2012CPOL 19.6K   5   2
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.

C#
[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:

XML
<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:

C#
 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

C#
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)


Written By
Software Developer Insight Software Solutions Lanka PVT Ltd
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhi Pin
beqiraj2545-Apr-12 22:52
beqiraj2545-Apr-12 22:52 
Thank you very muc for the tip
QuestionOut of memory for Big files Pin
Neeraj CodeP1-Apr-12 2:04
Neeraj CodeP1-Apr-12 2:04 

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

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