Dynamically set the enctype for uploading files

Source: Internet
Author: User
Tags button type

Dynamically set the enctype for uploading files


<FM id= "Up" name= "Up" action= "upfileile.php" method= "post" enctype= "Multipart/fm-data" >
<input type= "File" Name= "upfileilefile[]"/>
<input type= "File" Name= "upfileilefile[]"/>
<button type= "Submit" >upfileile</button>
</fm>
The person who has studied JS may try to use JS to check and control enctype type

Fm=document.getelementbyid (' Up ');
if (Fm.getattribute (' enctype ')!= ' Multipart/fm-data ') {
Fm.setattribute (' enctype ', ' multipart/fm-data ');
The above code is completely in the format of the
FF run well but IE is not compatible, other browsers can not be compatible, IE cannot be compatible
To see the Yui class library to be inspired
In IE, FM has no ENCTYPE attribute, only encoding attribute, then the above code is rewritten as:

Fm=document.getelementbyid (' Up ');
if (Fm.getattribute (' enctype ')!= ' multipart/fm-data ' &&fm.encoding!= ' multipart/fm-data ')
if (fm.encoding) {
Fm.setattribute (' Encoding ', ' multipart/fm-data ');
}else{
Fm.setattribute (' enctype ', ' multipart/fm-data ');
}
}
The most external if judgment can be removed because you are going to set it to upload files so you can try this code.
<script type= "Text/javascript" >
function upfileile (FM) {
if (fm.encoding) {
Fm.setattribute (' Encoding ', ' multipart/fm-data ');
}else{
Fm.setattribute (' enctype ', ' multipart/fm-data ');
}
Fm.setattribute (' method ', post ');
if (!fm.getattribue (' action ')) {
Fm.setattribute (' action ', location.href);
}
Fm.submit ();
}
</script>
&LT;FM action= "upfileile.php" onsubmit= "try{upfileile (this);} catch (e) {};return false; >
<input type= "File" Name= "upfileilefile[]"/>
<input type= "File" Name= "upfileilefile[]"/>
<button type= "Submit" >upfileile</button>
</fm>

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.