Click here to Skip to main content
Licence CPOL
First Posted 6 Mar 2006
Views 74,382
Downloads 1,262
Bookmarked 40 times

File Download using Web Service

By | 6 Mar 2006 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

D.Kannan



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow do you installed Web service on a particular server and select it and the reference? PinmemberMember 44038135:52 27 Jun '08  
QuestionDownload progress Pinmemberrikidude10:03 28 Mar '07  
QuestionError Pinmembershakti538520:20 28 Jan '07  
GeneralURL format file name Pinmembersumoncsekugmail19:14 22 Mar '06  
GeneralFile is empty Pinmemberbigmimmo23:13 7 Mar '06  
GeneralRe: File is empty PinmemberD.Kannan23:31 7 Mar '06  
GeneralRe: File is empty Pinmemberbigmimmo3:54 8 Mar '06  
GeneralRe: File is empty PinmemberRamamurthi18:53 25 Oct '07  
GeneralRe: File is empty Pinmemberram_west21:17 13 Oct '08  
GeneralRe: File is empty Pinmemberxujian12185820:36 25 Mar '10  
Generalreally helpful article Pinmembersam_19791:02 7 Mar '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 Mar 2006
Article Copyright 2006 by D.Kannan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid