Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

608
Views
Sending file from JQuery Ajax to C# Webmethod

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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");
    }
});}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!