Save (Insert) Image in Binary Format in database using ASP.Net Web Service

on Aug 16, 2021 12:28 AM

Hi Developers,

In my application i need to save a image to database as image format using web service.

How can i get uploaded image as image format and send to database to insert.

Kindly suggest me to do this module

Thanks and regards,


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Aug 17, 2021 12:18 AM

Hi paulrajmca,

Check this example. Now please take its reference and correct your code.


I have made use of the following table tblFiles with the schema as follows.

Download SQL file


<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button Text="Save" runat="server" ID="btnSave" />
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    $(function () {
        var reader = new FileReader();
        var fileName, type;
        $('[id*=fuUpload]').change(function () {
            var file = $(this)[0].files;
            if (typeof (FileReader) != "undefined") {
                fileName = file[0].name;
                type = file[0].type;
            } else {
                alert("This browser does not support HTML5 FileReader.");

        $("[id*=btnSave]").click(function () {
            var byteData = reader.result;
            byteData = byteData.split(';')[1].replace("base64,", "");
            var obj = {};
            obj.Data = byteData;
            obj.Name = fileName;
            obj.Type = type;
                type: "POST",
                url: "FileService.asmx/SaveFile",
                data: '{fileData : ' + JSON.stringify(obj) + ' }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    if (r.d == 1) {
                        alert("File Uploaded successfully.");
                error: function (r) {



using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
public class FileService : System.Web.Services.WebService
    public int SaveFile(FileData fileData)
        int i = 0;
        if (!string.IsNullOrEmpty(fileData.Name))
            byte[] fileBytes = Convert.FromBase64String(fileData.Data);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(constr))
                string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                    cmd.Parameters.AddWithValue("@Name", fileData.Name);
                    cmd.Parameters.AddWithValue("@ContentType", fileData.Type);
                    cmd.Parameters.AddWithValue("@Data", fileBytes);
                    i = cmd.ExecuteNonQuery();

        return i;

    public class FileData
        public string Name { get; set; }
        public string Data { get; set; }
        public string Type { get; set; }


Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
Public Class FileService
    Inherits System.Web.Services.WebService

    Public Function SaveFile(ByVal fileData As FileData) As Integer
        Dim i As Integer = 0
        If Not String.IsNullOrEmpty(fileData.Name) Then
            Dim fileBytes As Byte() = Convert.FromBase64String(fileData.Data)
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using conn As SqlConnection = New SqlConnection(constr)
                Dim sql As String = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)"
                Using cmd As SqlCommand = New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@Name", fileData.Name)
                    cmd.Parameters.AddWithValue("@ContentType", fileData.Type)
                    cmd.Parameters.AddWithValue("@Data", fileBytes)
                    i = cmd.ExecuteNonQuery()
                End Using
            End Using
        End If

        Return i
    End Function

    Public Class FileData
        Public Property Name As String
        Public Property Data As String
        Public Property Type As String
    End Class
End Class