Sending files via ajax to Webmethod

Asked

Viewed 25 times

0

Good afternoon! I’m trying to send files via ajax for a WebMethod in c#, but I keep getting back my whole html page

Javascript

//Upload de arquivos
$('#upload').change(function () {
    var fileUpload = $("#upload").get(0);
    var files = fileUpload.files; 

    // Create FormData object  
    var fileData = new FormData();
    // Looping over all files and add it to FormData object  
    for (var i = 0; i < files.length; i++) {
        fileData.append(files[i].name, files[i]);
    }  

    //chamando o webmethod
    $.ajax({
        type: 'POST',
        url: './Index.aspx/UploadArquivos',
        data: fileData,
        contentType: false, // Not to set any content header  
        processData: false, // Not to process data
        success: function (response) {
            console.log(response);
        }
    });
});

Webmethod

[WebMethod]
public static string UploadArquivos(FormDataCollection formData)
{
    //do stuff
    Debug.WriteLine("Worked");
    return "Response";
}

I’m running another call ajax on the same page and it works correctly, but the one I’m doing doesn’t even stop at the breakpoint I put inside Webmethod. What am I doing wrong?

I believe the problem is in any of the arguments at the time of the call, or even the kind of data I’m waiting for in my webmethod (maybe it’s not a FormDataCollection?), but I’ve been all over the Internet and I can’t find a solution to my problem...

inserir a descrição da imagem aqui

  • 1

    Are you sure you’re calling the right address? './Index.aspx/UploadArquivos it seems to me that you are asking for another page that does not have the WebMethod and is returning your Response standard including the MasterPage

  • Yes, it’s even the same address as the other ajax request from the page... I did a further search and it looks like it’s because the webmethod expects a json... So he’s not into the method

  • From where you concluded that he expects a json?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.