2

I want to send data from aspx page to .cs Webmethod using jQuery Ajax. my html code is as shown below

            <tr>
                <td>
                    <input type="text" id="txtName">
                </td>
            </tr>
            <tr>
                <td>
                    <form action="" id="AttachmentForm1" enctype="multipart/form-data">
                        <input type="file" name="UploadFile1" id="txtUploadFile1" class="btn btn-sm" />
                    </form>
                </td>
            </tr>

And my javascript code as below

 function saveData() {

    var file = $("#txtUploadFile1")[0].files[0];

    $.ajax({
        url: 'CCA_Form.aspx/SaveData',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({  Name: txtName, fileData: file }),
        success: function () {
            alert("Data Added Successfully");
        },
        error: function () {
            alert("Error while inserting data");
        }
    });}

And my c# code as below

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static bool SaveData(string Name, string[] fileData)
    {
        //Breakpoint
        return true;
    }

how can i get uploaded file in Webmethod in c#?

Thanks in advance.

2

1 Answer 1

-1

You need to do using XMLHttpRequest
This guys has a really nice tutorial about it. see it if it helps you: Click Here

Or

For your above code:

var filename = $("#txtUploadFile1").val();
 $.ajax({
    url: 'CCA_Form.aspx/SaveData',
    type: 'POST',
    enctype: 'multipart/form-data',
    data: {file: filename},
    success: function () {
        alert("Data Added Successfully");
    },
    error: function () {
        alert("Error while inserting data");
    }
});}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.