Upload file via a web method



Nowadays the web service based applications are going to be more useable. Both of the Desktop and Web Applications use the web services as their business logic layer. 

So, one of the important thing that a developer needs to do in his/her application is that the uploading a file to the application’s server. In this article, I will show you how to upload a file to the server via a web method in a SOAP web service.


About the 12 years ago I wrote the first application based on the Web Service. I can remember that one of the main challenges in that project was the approach to uploading files from the desktop client to the web server. Because we weren’t familiar with SOAP Web Service.

Using the code

The code is very very easy to use. As you can see, the solution contains 2 projects:

  1. Web Service
  2. Windows Client application

I am going to describe the web service fist. The web service has a simple web method that you can see below: 


    public bool UpuloadFile(string fileName, byte[] fileContent)
        string filesDirectory = ConfigurationManager.AppSettings["store_directory"];
        System.IO.File.WriteAllBytes(string.Format("{0}{1}", filesDirectory, fileName), fileContent);

        return true;

The method has 2 parameters, fileName that is a string and contains the file name and the file content that is an array of bytes. Also in the first line, I the code read the path that file should be stored on that. in the correct way, I should put the code on a try-catch statement.

At the client side application, We should approach in 3 steps:

1. Adding the web service as a web reference to the project. 

2. Adding a form or using the default form that you can find in the project. 

3. As you can see in the MainForm of the project this form has some additional code. But the main lines exist in the btnUpload_Click event handler. 

        private void btnUpload_Click(object sender, EventArgs e)
            FileService.FileService wsFileService = new FileService.FileService();

            byte[] contents = System.IO.File.ReadAllBytes(txtFilePath.Text);
            string fileName = System.IO.Path.GetFileName(txtFilePath.Text);

            wsFileService.UpuloadFileCompleted += WsFileService_UpuloadFileCompleted;
            wsFileService.UpuloadFileAsync(fileName, contents);

In the first line, I created an instance of the FileService. In next two lines, I read the selected file’s content as an array of bytes and read the name of the file that exists in a textbox I’ve filed it in another method.

So, in the next line, I recognize an event handler for the Completed event of the web method asynchronous calling. Asynchronous calling helps the application to call the web method in a new thread. In this case, the calling process is not interrupting the main thread of the application. 

And in the last line, we call the web method and pass two parameters.

If you review the WsFileService_UpuloadFileCompleted method, you will find the simple lines of code. It just called when the web methods calling finished and if the result( the result is a property that contains the return variable of the web method) is true, it shows a message box.

Points of Interest

In this simple code, you will find a useful way uploading a file to a web server from any kind of client(Desktop and Web)