Js dynamically inserts enctype = multipart/form-data on the form

Source: Internet
Author: User

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 ';

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.