0

The following setup triggers the "success" event, however the response is simply the content of the aspx page.

HTML

<input id="fileInput" type="file" />
<button type="button" onclick="UploadFile();">Upload File</button>

Javascript

function UploadFile() {
    var fileData = new FormData();
    fileData.append('file', $('#fileInput')[0].files[0]);

    $.ajax({
        type: 'POST',
        url: 'Upload.aspx/IngestFile',
        contentType: false,
        processData: false,
        data: fileData,
        success: function (response) {
            console.log(response);
        },
        error: function (response) {
            console.log(response);
        }
    });
}

C#

[System.Web.Services.WebMethod]
public static string IngestFile()
{
    return "Hello World";
}

As far as I can tell, the ajax call is never reaching the WebMethod. As removing the data and processData ajax properties and using contentType: 'application/json' returns the json object {d: "Hello World"} as I was hoping for from the original ajax, and hits the breakpoint in the C# code.

Using jQuery 1.8.3.

Tried in Edge 87.0.664.66 and IE11

The only message in the console is the html of the page as loaded

<!DOCTYPE html>
<html>
...
</html>
  • 3
    As this is more a server side issue (as the JS is correct) I added the relevant server-side tags to the question. I'm not familiar with WebMethods, however I would guess that the issue is because there is no argument to accept the file data in the method signature. – Rory McCrossan Jan 08 '21 at 11:53
  • It would also be worth checking the console in your browser to see if any errors are raised from the AJAX request that you can debug – Rory McCrossan Jan 08 '21 at 12:00
  • As far as I can tell the files are accessed though a property of the `Request` and all examples I have seen have no arguments for the `[WebMethod]`. There is no browser console messages at all other than the html content of the aspx page from the console.log I have added. – Richard Harris Jan 08 '21 at 12:17
  • 1
    I believe my question to be a duplicate of this one I have since found: [Question from 2016](https://stackoverflow.com/questions/40642286/ajax-send-formdata-c-sharp-webmethod). I don't have the points to close this off but I have flagged it as a duplicate (I think). – Richard Harris Jan 08 '21 at 14:13

0 Answers0