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:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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,371 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,371 Comments

@Sudar,

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


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

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


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

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


K V V Swami Naidu
Mar 19, 2009 at 1:30 AM // reply »
1 Comments

Thank you very much.


anon
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,371 Comments

@Anon,

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


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

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


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

Thanks, now working...


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

@Mon, @yoyok,

Awesome fellas. Glad to help.


Binial
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 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »