Click here to Skip to main content
15,890,579 members
Articles / Operating Systems / Windows
Article

File Download using Web Service

Rate me:
Please Sign up or sign in to vote.
2.95/5 (17 votes)
6 Mar 2006CPOL2 min read 181.5K   4.8K   46   13
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:

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

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPossible to download file in web service itself instead of return bytes or file path in web service download method ? Pin
Aravindba4-Feb-24 16:40
professionalAravindba4-Feb-24 16:40 
Hi, i can download file when return file path or byte or stream and download file in browser can, but i want to download file in web service method itself. i am using following code to download but i get an error
Client found response content type of 'application/octet-stream', but expected 'text/xml'.
The request failed with the error message:
"

VB
'Download function in web service
       <WebMethod(EnableSession:=False)>
    Public Function DownloadFE() As Byte()
        Try

            Dim filePath As String = HttpContext.Current.Server.MapPath("~/App_Data/sample.pdf")
            Dim fileContent As Byte() = File.ReadAllBytes(filePath)
            ' Set response content type and headers for file download
            HttpContext.Current.Response.ContentType = "application/octet-stream"
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=sample.pdf")

            ' Write the file content to the response stream
            HttpContext.Current.Response.BinaryWrite(fileContent)
            HttpContext.Current.Response.Flush()
            HttpContext.Current.Response.End()
            Return fileContent

            Catch ex As Exception

        End Try
    End Function

Regards
Aravindb

GeneralIts working Fine Pin
abhi.p24-Sep-13 19:29
abhi.p24-Sep-13 19:29 
QuestionHow do you installed Web service on a particular server and select it and the reference? Pin
Member 440381327-Jun-08 5:52
Member 440381327-Jun-08 5:52 
QuestionDownload progress Pin
rikidude28-Mar-07 10:03
rikidude28-Mar-07 10:03 
QuestionError Pin
shakti538528-Jan-07 20:20
shakti538528-Jan-07 20:20 
GeneralURL format file name Pin
sumoncsekugmail22-Mar-06 19:14
sumoncsekugmail22-Mar-06 19:14 
GeneralFile is empty Pin
bigmimmo7-Mar-06 23:13
bigmimmo7-Mar-06 23:13 
GeneralRe: File is empty Pin
D.Kannan7-Mar-06 23:31
D.Kannan7-Mar-06 23:31 
GeneralRe: File is empty Pin
bigmimmo8-Mar-06 3:54
bigmimmo8-Mar-06 3:54 
GeneralRe: File is empty Pin
Ramamurthi25-Oct-07 18:53
Ramamurthi25-Oct-07 18:53 
GeneralRe: File is empty Pin
ram_west13-Oct-08 21:17
ram_west13-Oct-08 21:17 
GeneralRe: File is empty Pin
xujian12185825-Mar-10 20:36
xujian12185825-Mar-10 20:36 
Generalreally helpful article Pin
sam_19797-Mar-06 1:02
sam_19797-Mar-06 1:02 

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.