Js dynamically inserts enctype = multipart/form-data on form _ form Effects

Source: Internet
Author: User
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 ';
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.