Monday, April 13, 2009

Making File Uploads Work in IE6 and 7

So, I'm developing a web application at home with Linux OS and Firefox web browser. Then I deploy it on a Windows OS and a IE Web Browser.


Yeah I know how dumb and risky it sounds, problem is I can't afford a new computer right now, so I use Ubuntu 8.10 for developing, and the target server for my developed web app is Windows.

You know cross-browser compatibility is always a headache, and this it's all true for File Uploads.

I use PHP, and for some reason, the file upload feature wouldn't work in IE Browser. WHYYYY!!

I spent almost 3 hours on forums and finally found the reason: ENCTYPE. All file upload features warn you: You have to change your form enctype attribute to "multipart/form-data" in order that the file input contents can travel through the request.


As I use an admin page with header, left navigation pane and content areas, I use the same form for several different features. And I have a feature which uploads files.

Hence, I dynamically change the ENCTYPE attribute through javascript. And after hours of trying to explain myself "why in the world is this not working if I'm doing everything right", and just before I start blaming the elves or believing I was coursed, I found this page:

It turns to be that for Internet Explorer, the W3C STANDARD enctype attribute is NOT ENOUGH. I have to set this particular ENCODING attribute as well, so my code ended up like this:

form.enctype = "multipart/form-data";
form.encoding = "multipart/form-data";

Yeah, I addded the form.encoding=... line and it worked perfectly.

Thanks Wildhoney @ talkphp forum!

Now I am compelled to spread the words of wisdom to all you programmers that have lost hope. No, you're not cursed, you just didn't know.

PS. Yes, my writing style today is a little bit crazy, that's what's left of an stressed mind after 3 hours of trying to find a logical reason to an error that shouldn't be there in the first time. Now I'll take some rest and no more coffee for me.


No comments: