Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to post file in MVC by use of 'formdata' which is not supported in IE9. please suggest any alternative to this.
Code:

var formData = new FormData($('#profileForm')[0]);
           if (actionName == "Save") {

               $.ajax({
                   url: '@Url.Action("AddProfile", "ProductProfile")',
                   type: "POST",
                   dataType: 'text',
                   contentType: false,
                   processData: false,
                   cache: false,
                   data: formData,
                   success: function (response) {
                       if (response != "Success") {
                           $('#errreportname').text(response);
                           setTimeout(function () { $('#errreportname').text(''); }, 3000);

                       }
                       else {
                           $('#closebtn').click();
                           GetListOfProfile();
                           clear();
                       }
                   },
                   error: function () {
                       $('#error').text("Unable to create the record");
                       setTimeout(function () { $('#error').text(''); }, 3000);
                   }
               });
           }
Posted
Comments
Sinisa Hajnal 10-Mar-15 6:56am    
Any array? JSON?
dirtyshooter 10-Mar-15 7:52am    
dint get you?
dirtyshooter 10-Mar-15 7:53am    
i have a file to post in the form too.

1 solution

FormData can be very convenient (https://developer.mozilla.org/en-US/docs/Web/API/FormData[^]), but in fact, with Ajax, it is absolutely not needed. Moreover, the form itself is needed. In fact, the <form> element is only mandatory when it is used with the "Submit" button, which automatically sends all key-value pairs in HTTP request, depending on the attribute values of the attribute "name" and the values of all controls inside the <form> element.

Even though you can use it all with Ajax and FormData, this is totally irrelevant to Ajax. You can directly get all name-value pairs from control, in particular, from jQuery wrappers of the control elements, using jQuery .val():
http://api.jquery.com/val[^].

And the name part of the name-value pair could anything, based on attributes name or not. It does not matter where you get the controls, are they in some form or not. All you need is to have them all on the page. You can obtain the jQuery wrapper using its selectors:
http://api.jquery.com/category/selectors[^],
in particular, http://api.jquery.com/id-selector/[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900