Skip to content

Cannot be used for AJAX form submission in IE7/8, as relies on forEach #2

@BMSKiwi

Description

@BMSKiwi

As per title. I fixed this for my own use (although files still don't work obviously) and figured I should post it. Simplified the formatting a little as well.

(function(w){
    if (w.FormData)
        return;
    function FormData(){
        this.fake = true;
        this.boundary = '--------FormData' + Math.random();
        this._fields = [];
    }
    FormData.prototype.append = function(key, value){
        this._fields.push([key, value]);
    }
    FormData.prototype.toString = function(){
        var boundary = this.boundary;
        var body = '';
        for(var i = 0; i < this._fields.length; i++){
            if (this._fields[i][1].name) {
                var file = this._fields[i][1];
                body += 'Content-Disposition: form-data; name="'+ this._fields[i][0] + '"; filename="' + file.name + '"\r\n';
                body += 'Content-Type: ' + file.type + '\r\n\r\n';
                body += file.getAsBinary() + '\r\n';
            }
            else{
                  body += '--' + boundary + '\r\n';
                  body += 'Content-Disposition: form-data; name="' + this._fields[i][0] + '";\r\n\r\n';
                  body += this._fields[i][1] + '\r\n';
            }
        };
        body += '--' + boundary + '--';
        return body;
    }
    w.FormData = FormData;
})(window);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions