Vayas
Vayas

Reputation: 213

FileUpload formData in ASP.NET WebForms

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!

Upvotes: 1

Views: 3635

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156948

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.

Upvotes: 0

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

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) {

    }
});

Upvotes: 1

Related Questions