Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData is not defined - Firefox 3.6.28 alternative

I have the great job of having to finish off a job originally given to a contractor but was never completed. Not a problem however I've now been told that the system must support Firefox 3.6! Not great but not something I would lose sleep over until now! The system has a Ajax function that uses the FormData object and then uploads a document (usually a PDF). I've ran this through Firefox 3.6 and I get the following

"FormData is not defined"
var formData = new FormData($('form')[0]);

That's fine as I can see that this object isn't supported, I just need to use a different method or means of collection... I used this:

var formData = Components.classes["@mozilla.org/files/formdata;1"]
        .createInstance(Components.interfaces.nsIDOMFormData);

However this gave me the following error!

Permission denied for http://10.29.100.23:8080 to get property XPCComponents.classes

I was unsure why this was... is the path "@mozilla.org/files/formdata;1" incorrect? I did more research and was getting nowhere! So I then thought about serializing the form changed the following to...

var formData =  {};

$.each($('form')[0].serializeArray(), function(_, kv) {
     if (formData.hasOwnProperty(kv.name)) {
         formData[kv.name] = $.makeArray(formData[kv.name]);
         formData[kv.name].push(kv.value);
     }else {
        formData[kv.name] = kv.value;
    }
});

although this didn#t error the Ajax function wasn't uploading (I presume it wasn't recognizing or finding the file or it was just collecting a string for the file value). Does anyone have any recommendations on an alternative for FormData in older browsers, especially Firefox 3.6 - that's the only old browser I have to support.

** update ****

this is the content of the form on the HTML page

<form action="" method="post" enctype="multipart/form-data" name="uploadForm" id="uploadForm" target="#">
    <label for="fileField">Rechnung hochladen</label>
    <input type="file" name="fileField" id="fileField">
    <progress id="progressbar" class="progressbar_margin hidden"></progress>
</form>
like image 254
Mike Sav Avatar asked May 09 '12 10:05

Mike Sav


1 Answers

FormData is an XMLHttpRequest Level 2 interface that makes it easy to submit a form (including file uploads) using XHR / Ajax. As you've discovered, it's only available in Firefox from version 4 onwards. (The MDN documentation has a browser compatibility table.)

I suggest trying the jQuery Form Plugin. It supports an iframe fallback for uploading files in older browsers.

like image 63
Jeffery To Avatar answered Oct 16 '22 17:10

Jeffery To