Setting Form EncType Dynamically To Multipart/Form-Data In IE (Internet Explorer)

Posted June 27, 2008 at 10:07 AM

Tags: Javascript / DHTML

A while back, I posted an AJAX-Style file upload demo. While using that technique in a current project, I discovered a bit of a bug. In FireFox, you can dynamically set the EncType of a form element to be "multipart/form-data" for file uploads; however, this does not work in Internet Explorer (IE). Apparently in IE, you have to set the "encoding" of the form rather than the "enctype". The good news is, you can set both values without concern and this will take care of the problem:

 Launch code in new window » Download code as text file »

  • // Using jQuery, set both the enctype and the encoding
  • // attributes to be multipart/form-data.
  • $( "form#upload-form" )
  • .attr( "enctype", "multipart/form-data" )
  • .attr( "encoding", "multipart/form-data" )
  • ;

Minor note, but wanted to write this down to help me remember.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:




Reader Comments

Jun 27, 2008 at 12:43 PM // reply »
102 Comments

Does firefox also work with "encoding" rather than just "enctype"? Would save you an extra line :).


Jun 27, 2008 at 12:47 PM // reply »
6,516 Comments

@Gareth,

I actually didn't even check that.

The angel in me would prefer to stay with "enctype" as that is what the name of the attribute actually is and therefore what the real "intent" of the attr() function is meant to be.... but if all browsers worked with "encoding", the devil in my might not care ;)


Feb 11, 2009 at 8:59 AM // reply »
1 Comments

Thanks Ben for documenting this behavior of IE. I was breaking my head for quite sometime to find out why my AJAX file upload is not working in IE.

Regards
Sudar


Feb 11, 2009 at 10:08 AM // reply »
6,516 Comments

@Sudar,

Glad to help! Yeah, it was really frustrating coming up against this for the first time.


Mar 13, 2009 at 9:23 AM // reply »
1 Comments

in opera and firefox also work whit "encoding". Thanks for the tip!


Mar 13, 2009 at 9:28 AM // reply »
1 Comments

thanks for the tip. this has saved me hours of recoding!


Mar 19, 2009 at 1:30 AM // reply »
1 Comments

Thank you very much.


May 12, 2009 at 11:36 PM // reply »
5 Comments

Ahh, I really wish this was higher up in Google when search terms are something like "ie dynamically create upload form" or "ie upload form bug", woulda saved me a lot of time. Thanks for documenting this.


May 13, 2009 at 7:29 AM // reply »
6,516 Comments

@Anon,

Well, I'm glad you made it here eventually, even if a little delayed :)


May 15, 2009 at 4:44 PM // reply »
1 Comments

Thanks, helped one I had the inspiration to google it!


Aug 14, 2009 at 12:21 PM // reply »
1 Comments

Thanks, now working...


Aug 17, 2009 at 2:10 PM // reply »
6,516 Comments

@Mon, @yoyok,

Awesome fellas. Glad to help.


Aug 23, 2009 at 11:12 PM // reply »
1 Comments

Thanks for the useful post, save me lots of time.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »