In this article I will explain with an example, how to upload file using Web Service (ASMX) in ASP.Net using C# and VB.Net.
First the file will be uploaded using FileUpload control and then the uploaded file will be converted to Byte Array and finally the Byte Array will be passed to the Web Service (ASMX) in ASP.Net using C# and VB.Net.
 
 
Web Service
Following is the Web Service which will be used for uploading files. The Web Service consists of a method named UploadFile which accepts the following parameters.
1. fileName – Name of the File.
2. contentType – Content Type of the File.
3. bytes – File data in Byte Array format.
The Byte Array is saved as File in folder using the WriteAllBytes method of the File class.
C#
using System;
using System.Web;
using System.Web.Services;
using System.IO;
 
///<summary>
/// Summary description for WebServiceCS
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class UploadService : System.Web.Services.WebService {
 
    public UploadService()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
 
    [WebMethod]
    public void UploadFile(string fileName, string contentType, byte[] bytes)
    {
        //Save the Byte Array as File.
        string filePath = Server.MapPath(string.Format("~/Uploads/{0}", fileName));
        File.WriteAllBytes(filePath, bytes);
    }
}
 
VB.Net
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
 
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class UploadService
    Inherits System.Web.Services.WebService
 
    <WebMethod()> _
    Public Function UploadFile(fileName As String, contentType As String, bytes As Byte()) As String
        'Save the Byte Array as File.
        Dim filePath As String = Server.MapPath(String.Format("~/Uploads/{0}", fileName))
        File.WriteAllBytes(filePath, bytes)
        Return Nothing
    End Function
End Class
 
 
Adding Web Reference of Web Service
Next thing you need to do is add the Web Reference of the Web Service so that it can be used on the ASP.Net Web page.
Please refer the following article for instructions in adding Web Reference of Web Service: How to add reference of Web Service (ASMX) in ASP.Net using Visual Studio.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control and a Button.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" />
 
 
Uploading File using Web Service in ASP.Net
When the Upload Button is clicked, the File name and the Content Type values are fetched. Then the File is converted into a Byte Array using ReadBytes method of the BinaryReader class.
Finally all the parameters are passed to the UploadFile method of the Web Service.
C#
protected void Upload(object sender, EventArgs e)
{
    //Get the name of the File.
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
 
    //Get the content type of the File.
    string contentType = FileUpload1.PostedFile.ContentType;
 
    //Read the file data into Byte Array.
    BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
    byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
 
    //Call the Web Service and pass the File data for upload.
    ServiceReference.UploadService service = new ServiceReference.UploadService();
    service.UploadFile(fileName, contentType, bytes);
}
 
VB.Net
Protected Sub Upload(sender As Object, e As System.EventArgs)
    'Get the name of the File.
    Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
 
    'Get the content type of the File.
    Dim contentType As String = FileUpload1.PostedFile.ContentType
 
    'Read the file data into Byte Array.
    Dim br As BinaryReader = New BinaryReader(FileUpload1.PostedFile.InputStream)
    Dim bytes As Byte() = br.ReadBytes(CType(FileUpload1.PostedFile.InputStream.Length, Integer))
 
    'Call the Web Service and pass the File data for upload.
    Dim service As New ServiceReference.UploadService()
    service.UploadFile(fileName, contentType, bytes)
End Sub
 
 
Downloads