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

Upload File Via a Web Method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
21 Jun 2017CPOL2 min read 22.9K   578   13  
In this sample code, we are going to upload a file via a web method asynchronously.

Introduction

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 things that a developer needs to do in his/her application is 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.

Background

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

C#
[WebMethod]

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, in the code read the path that file should be stored on that. In the correct way, I should put the code in 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.

    Image 1

  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.
    C#
    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 the 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 is 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 of uploading a file to a web server from any kind of client (Desktop and Web).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Mehdi Mohseni is a Software senior developer (Consultant) in Sigma ITC (www.sigmait.se), Mehdi has a deep experience in N-Tier software applications as well as MVC design pattern. Mehdi has led more than 100 Asp.Net C# or VB.Net Automation applications. Mehdi is working in Toyota Material Handling Logistic Solution as Senior .Net Developer now.

Comments and Discussions

 
-- There are no messages in this forum --