Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Download using Web Service

0.00/5 (No votes)
7 Mar 2006 5  
An article on file download using Web service

Introduction

I was thinking of writing a sample program that can help download a file from a remote server to a local machine through Web services. We can achieve this in more than one way. However I wish to achieve this using .NET Web services. I have utilized System.IO objects to achieve this. Hopefully this will be helpful. This gives only the basic functionalities of file downloading using Web services.

Description

This contains the creation of a Web service and a Web client.

First let us create a Web service. Create a new C# Web service project and name it Web service- File Download. Now rename the Service1.asmx to FileDownload.asmx. Create a new Web method called DownloadFile. Now cut and paste the following code into that:

[WebMethod()]
public byte[] DownloadFile(string FName)
{
    System.IO.FileStream fs1=null;
    fs1=System.IO.File.Open(FName,FileMode.Open,FileAccess.Read);
    byte[] b1=new byte[fs1.Length];
    fs1.Read(b1,0,(int)fs1.Length);
    fs1.Close();
    return b1; 
}

Compile this code and release using IIS.

Let us create a Web client to access this Web service. Create one C# Web application and name it as WS File Transfer client. In the Web form1, create one button and name it as File download and create one label and name it as label1.

In the button click event, please cut and paste the following code:

private void Button1_Click(object sender, System.EventArgs e)
{
    System.IO.FileStream fs1=null;
    WSRef.FileDownload ls1 = new WSRef.FileDownload();
    byte[] b1=null;
    b1 = ls1.DownloadFile("C:\\Source.xml");
    fs1=new FileStream("D:\\Source.xml", FileMode.Create);
    fs1.Write(b1,0,b1.Length);  
    fs1.Close();
    fs1 = null;
    Label1.Text="File downloaded successfully";
}

Now add a new Web reference; add the reference of the Web service which you had just created. Compile and run the code.

In the runtime mode, clicking on the button will transfer the file from the server to client and display the message “File Downloaded Successfully”.

Web Reference

If the server is a different machine than the client machine, then while giving a Web reference to the client, please select the Web service installed on that particular server and select it and the reference. The code is tested both on Intranet and Internet and works fine.

Finally

As I mentioned earlier, this article is very basic. This needs to be improved a lot to make it professional. However, the following points are worth noting for further enhancement:

  1. Web service request and response to be added.
  2. File encryption and decryption to be added.
  3. Reading of filename and path can be configurable. This configuration file could be a *.xml file or a *.txt file or even database fields.

Feel free to comment on this and modify as you like.

Happy programming.

Cheers!

History

  • 7th March, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here