When uploading files, we encountered a strange problem. IE6, 7, and 8 always failed to upload files, and the server could not receive files. We all knew that we wanted form to be able to submit files, you need to specify the attribute of enctype = multipart/form-data 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,
The Code is as follows:
@ Using (Html. BeginForm ()){
}
There is It is used to upload files and does not want to modify the Html. BeginForm () of the parent page for this PartialView. In PartialView, I add enctype to form by using scripts:
The 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:
The Code is as follows:
$ (Function (){
$ ('# File '). parents ('form '). attr ('enablesype ', 'multipart/form-data '). get (0 ). encoding = 'multipart/form-data ';
});