Skip to main content
Ben Nadel at the New York ColdFusion User Group (May. 2008) with: Michael Dinowitz
Ben Nadel at the New York ColdFusion User Group (May. 2008) with: Michael Dinowitz ( @mdinowitz )

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

By on

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:

// 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.

Want to use code from this post? Check out the license.

Reader Comments

15,674 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 ;)

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

6 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.

3 Comments

Hi Ben, Again, great site. I've got another question for you. I'm trying to upload a file from flex via remoteobject with amf.

Now the coldfusion file (actually cfc method) that I'm supposed to work with used to work with a form field, html style; Using <cffile formfield= ... etc. When I'm in flex I have the file as a bytearray, but can change it to other data types if needed (string etc.). Blah Blah, here is the question. Do you know what format coldfusion is looking for when it grabs the form field that holds the reference to the file? I can't seem to find any documentation on this and I need to know it so I can try and get flex to convert to that format first and then send it as a variable to the cfc.

15,674 Comments

@Josh,

I'm not really versed in FLEX. When a request comes into ColdFusion, I believe it just grabs the byte array of the request body and parses out the multi-part information based on delimiters. But, I don't know much about that, technically.

2 Comments

Ben,

I found out that IE uses 'image/pjpeg' instead of 'image/jpeg'. As a matter of fact, enctype="multipart/form-data" works for ALL browsers for as long as you check for what it returns. IE had to return something else :))

Regards,
Ravi.

1 Comments

Thanks for sharing this. It really helped me in fixing my issue. I was able to find that IE is not sending the file and using wrong Content-Type while posting data. Your post helped me in solving it. Thank you.

1 Comments

Thank you. I am created the form in JavaScript and set EncType in 'multipart/form-data', but Internet Explorer do not understand this property and sended this form with enctype in 'application/x-www-form-urlencoded'. Now I've added encoding and everything started working normally.

1 Comments

Very helpful ... I had this problem using Eclipse build-in browser but set the encoding property solved the issue ... Thanks a lot

1 Comments

THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU!

I banged my head on this problem for half a week before I stumbled across this little gem...

1 Comments

Hi Ben,

In Internet Explorer 8.0.6001.18702, I faced a problem of access to the property "enctype" of the form.
To solve the problem, I used the following line of code:
$ ('form # upload-form'). get (0). encoding = 'multipart/form-data';

To remove the encoding, use 'text/plain' for example.

Congratulations for this site!
Best regards

1 Comments

i am using VS 2010
when i i remove this line
//form.attr("enctype", "multipart/form-data");
from the code. the iframe.load triggers but the file dos not reach the server.
on the other hand if i leave this line.
the files received by the server but the even will not be triggered.

any help please.

1 Comments

Use jQuery attrHook to solve the pb!

$.attrHooks.enctype = {
get: function( elem ) {
return $.nodeName( elem, 'form' ) ?
elem.encoding || elem.enctype :
null;
}
,set: function( elem, value ) {
if ( $.nodeName( elem, 'form' ) && elem.encoding )
elem.encoding = value;
}
};

1 Comments

I prefere do this:

$('input[type=\"file\"]').each(function(i, it) {
$(it).closest('form').attr('enctype', 'multipart/form-data').attr('encoding', 'multipart/form-data');
});

this code stays at my global.js an any form ho had file input be right.

2 Comments

Hello ben.
I'm taking enctyple problem and i didnt set yet. How can i set my codes ? Can you help me please? My codes are;

$.ajax({
type: 'POST',
url: 'resimekle.asp',
data: $('#resimekleme').serialize(),
success: function(ajaxCevap2) {
$('#resimeklesonuc').html(ajaxCevap2);
}
});

I must add enctype to date area. How can we do this ? Thank you before your help.

1 Comments

Hello Bilal,

I think you are looking for this:
$.ajax({
type: 'POST',
url: 'resimekle.asp',
data: $('#resimekleme').serialize(),
},
contentType:'application/x-www-form-urlencoded; charset=UTF-8',
success: function(ajaxCevap2) {
$('#resimeklesonuc').html(ajaxCevap2);
}
});

See you around Charlotte soon :)
Henry

2 Comments

@Henry,
First of all thank you for your help. But it doent work. I tried again and took same error.

POST http://localhost/bartu2/adminpanel/resimekle.asp 500 (Internal Server Error) uygulamaresim.asp:19
o.extend.ajax uygulamaresim.asp:19
rle uygulamaresim.asp (2):4
onclick

All my codes are here. Can you check again for me ?

form.asp

<script language="javascript">
function rle(){
$("#resimeklesonuc").html('<img src="../imgs/loading.gif" />');
$.ajax({
type: 'POST',
url: 'resimekle.asp',
data: $('#resimekleme').serialize(),
contentType:'application/x-www-form-urlencoded; charset=UTF-8',
success: function(ajaxCevap2) {
$('#resimeklesonuc').html(ajaxCevap2);
}
});
}
</script>

<h2>Resim Ekle</h2>
<form id="resimekleme" method="post" enctype="multipart/form-data">
<div>Resim Aç?klamas? : </div><input name="resimaciklama" type="text" style="width:200px;" /><br/>
<div>Resim : </div><input name="dosya" type="file" /><br/>
<input name="resimekle" value="Resim Ekle" onclick="rle()" type="button" />
</form>
<div id="resimeklesonuc"></div>

resimekle.asp

<%
Set upload = server.CreateObject("Persits.Upload")
upload.save
aciklama=upload.Form("resimaciklama")
on error resume next
set dosya=upload.Files("dosya")
response.Write dosya.filename
%>

Thank you

1 Comments

Is there any issue with setting the default enctype of a form to multipart/form-data if there are no files uploaded? I am making a form that uses a flash system for uploading and falls back to a regular file input for non flash users. I was going to default the form to allow for the file uploads but I suspect the majority of my users will have flash running on their system.

Great site Ben, I keep stumbling across it and now you are bookmarked :)

1 Comments

Thanks a lot.

After struggling for a day came across your blog. It just resolved my issue within minute.

Appreciate your effort for providing solution and publishing it on the web.

1 Comments

Hi,

I want to upload multiple files in one shot. I was using FormData component. Now problem is FormData is not supporting in IE... Can you please help me in this?

Thanks in advance.

Thanks,
Pritee

1 Comments

Was struggling with IE for 2 days. Frustration started replacing Patience. If I had found this solution two days ago, life would have been easier; but mostly I might not have appreciated value of knowledge over information.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel