1

I have some problem with posting formData to server side action method. Because ajax call doesn't send files to server, I have to add file uploader data to formData manually like this
It is impossible to call a server method

[WebMethod]
    public HttpPostedFileBase Name(HttpPostedFileBase file)
    {
        string ret = "test";
        return file;
    }

Errors on the client side no I wrote jQuery function that need to post form data to server using ajax call. this is my script:

data.append(self.idFileInput, file[f]);
    $.ajax({
        type: "POST",
        url:  "/AddContract.aspx/Name",            
        data: data,
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function (data) {

        }
    });

Any tips, link or code example would be useful.
Thank you in advance!

1
  • string works, but I need to upload files using FormData JS Commented Feb 15, 2014 at 21:05

2 Answers 2

1

try to use contentType: 'application/json; charset=utf-8',

$.ajax({
    type: "POST",
    url:  "AddContract.aspx/Name",            
    data: { field1: self.idFileInput, field2 : file[f]} ,
    dataType: 'json',//Remove this line this line is causing issue.
    contentType: 'application/json; charset=utf-8',
    processData: false,
    success: function (data) {

    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

At back end you need to get these params from Request["feild1"]
How is It? Can example?
Server method is not called at all if I use FormData. If you send a string everything works fine
here is a good example you can have a look. codeproject.com/Articles/13700/…
0

In a previous answer I said something stupid about ASPX not supporting WebMethod calls, which they do.

Now a real answer:

In order to post a file you need to use the ajaxSubmit method. See this reference.

1 Comment

Unfortunately, no. I use iframe, but it is advisable FormData Html5. FormData works in MVC, but WebForms no

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.