0

I am trying to Send Image using Ajax Call and transfer code behind web method asp.net c#.

Below is my script.

<script type="text/javascript">
    function SubmitFunction() {
        alert(imgData_Based64String);
        imgData_Based64String = "test";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "index.aspx/SaveImage",
            data: "{ 'Based64BinaryString' :'" + imgData_Based64String + "'}",
            dataType: "json",
            success: function (data) {
                alert("success");
            },
            error: function (result) {
                alert("Error");
            }
        });
    }

</script>

Below is my image and button code.

<img src="img.jpg" id="img" width="100px" height="100" />
<asp:Button ID="btnCapture" Text="Capture" runat="server" OnClientClick="SubmitFunction();"
        OnClick="btnCapture_Click" />

Code behind C#.

[System.Web.Services.WebMethod]
public static void SaveImage(string Based64BinaryString)
{
    string Value = Based64BinaryString;       
}

Actually I am new into to Ajax call, Image should convert to binary format and transfer to web method string value. Also Image ID is not declaring into the above script code to convert binary format. What I am trying one image is there and that Image to transfer to a SQL table.

Mr doubt
  • 51
  • 1
  • 10
  • 42
  • It really isn't clear what exactly the problem is. What exactly doesn't work? – Dekel Oct 01 '16 at 13:45
  • if you're trying to post an image the content type cannot json. – derloopkat Oct 01 '16 at 13:48
  • I want to transfer image to code behind web method to stored into sql table. @Dekel – Mr doubt Oct 01 '16 at 13:50
  • Then how to transfer image using ajax call to web method. @derloopkat – Mr doubt Oct 01 '16 at 13:54
  • Check this http://stackoverflow.com/questions/14575787/how-to-upload-image-in-asp-net-mvc-4-using-ajax-or-any-other-technique-without-p You are probably using a webservice and this code is for MVC but the jQuery call can be useful. – derloopkat Oct 01 '16 at 13:54
  • I did not work into MVC and I know only using asp.net web forms. @derloopkat – Mr doubt Oct 01 '16 at 13:55
  • If possible can you update your answer. @derloopkat – Mr doubt Oct 01 '16 at 13:59
  • I am trying to stored this image: into sql server table using ajax call and web method, hope you understand now. @Dekel – Mr doubt Oct 01 '16 at 14:01
  • @zahed, you said what you want, but not what doesn't work. I don't understand if your problem is with the javascript, the c# or the sql. – Dekel Oct 01 '16 at 14:09
  • @zahed Sorry, that's a different thing. You don't have an input tag for uploading the image but an img tag with the image that has to be uploaded. Now your code makes sense. – derloopkat Oct 01 '16 at 14:09
  • is it okay to send src property of the image to the server and get the image from there? – derloopkat Oct 01 '16 at 14:20
  • Ok no problem can you update answer. @derloopkat – Mr doubt Oct 02 '16 at 03:35

1 Answers1

0
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#falseinput').attr('src', e.target.result);
            var data = {};
            data.image = e.target.result;
            $.ajax({
                type: "POST",
                url: "someWebForm.aspx/SomeWebMethod",
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    //do something
                }
            });
            return false;
        };
        reader.readAsDataURL(input.files[0]);
    }
}

 [System.Web.Services.WebMethod]
        public static string SomeWebMethod(string image)
        {
            MemberImageData = image;

            return MemberImageData;
        }
Aleksey
  • 33
  • 8