We all know that to allow the form to submit files, you must specify enctype = multipart/form-data attribute on the form to upload files. There are many articles about enctype, so we will not explain them any more.
The problem is that the MVC Html. BeginForm () is used to output the form code. By default, enctype is not added,
Copy codeThe Code is as follows: @ using (Html. BeginForm ()){
}
In PartialView, there is a <input type = "file"/> used to upload files, and you do not want to modify the Html of the parent page for this PartialView. beginForm (). My approach is to add enctype to form using scripts in PartialView:
Copy codeThe Code is as follows: $ (function (){
$ ('# File'). parents ('form'). attr ('enablesype', 'multipart/form-data ');
});
The latest mainstream browsers have passed the test, but several old versions of IE have problems. adding enctype to BeginForm () can solve the problem, indicating that the problem lies in the above script. Multi-party searches finally found that the question appeared in IE6, 7, and 8. Direct attr ('enablesype ', 'multipart/form-data') is not supported '), instead, set the dom attribute encoding = 'multipart/form-data' and change the final script code:
Copy codeThe Code is as follows: $ (function (){
$ ('# File '). parents ('form '). attr ('enablesype ', 'multipart/form-data '). get (0 ). encoding = 'multipart/form-data ';
});