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  |  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 »
106 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 »
7,539 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 »
7,539 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 »
7,539 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 »
7,539 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.


Feb 6, 2010 at 4:47 PM // reply »
1 Comments

The right-and-strict implementation should be:

$('form#upload-form').attr((this.encoding ? 'encoding' : 'enctype') , 'multipart/form-data');

Cheers!


Feb 6, 2010 at 4:56 PM // reply »
7,539 Comments

@Emanuele,

Good point, that is probably technically more accurate as it only applies attributes when necessary.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 10:28 PM
Posting XML SOAP Requests With jQuery
can you please point me to the jquery documentation on the following # // Create our SOAP body content based off of # // the template. # var soapBody = soapTemplate.html().replace( # new RegExp( "\\ ... read »
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »
Mar 18, 2010 at 11:15 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Hi Ben, nice to have you back! I already gave up on you, thinking you'll write about jQuery and iPhone for the rest our our lives! :) ... read »