Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

javascript - FormData in IE8/9

i have implemented this script for uploading files with ajax, it works perfect in other browsers except for explorer, i noticed that formData is not supported by IE9 and less, are there any alternatives for formData in IE, and i want use clean javascript

    function doObjUploadExplorer(url, lnk_id, file, progress, success, content, frm, div_dlg, start_func){
    var file_input = null,
      frm_data = new FormData(),
      req;

    try {
        //firefox, chrome, safari etc
        req = new XMLHttpRequest();
    }

    catch (e) {
        // Internet Explorer Browsers
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }


if (document.getElementById(file)) {
    file_input = document.getElementById(file);

    for (var i = 0; i < file_input.files.length; ++i) {
        frm_data.append(file, file_input.files[i]);
    }
}

req.upload.addEventListener('progress', function(e) {  //Event called while upload is in progress
    if (progress !== undefined
            && e.lengthComputable) {
        $('#' + progress).html('<font>Uploading... ' + Math.round((e.loaded / e.total) * 100) + '%</font>');
    }
});

req.upload.addEventListener('load', function(e) {  //Event called when upload is completed
    $('#' + progress).html('<font>Retrieving updated data...</font>');
});

req.upload.addEventListener('error', function(e) {  //Event called when an error is returned by the server
    alert('An error has occurred...');
});

req.addEventListener('readystatechange', function(e) {        
    if (this.readyState === 4) {
        if (this.status === 200) {
            if (content !== undefined) {
                $('#' + content).html(this.response);
            }

            if (success !== undefined) {
                showChkMark(success);
            }
        } else {
            console.log('Server replied with HTTP status: ' + this.status);
        }


        if (progress !== undefined) {
            $('#' + progress).hide();
        }

        if (div_dlg !== undefined) {
            $('#' + div_dlg).dialog('close');
        }

        $('#' + file)
        .attr('disabled', false)
        .val('');
    }
});

if (progress !== undefined) {
    $('#' + progress).show();
}

$('#' + file).attr('disabled', true);
url += (
        url.indexOf('?') === -1
        ? '?'
        : '&'
    );
url += 'lnk_id=' + lnk_id + '&file=' + file;
req.open('POST', url);
req.setRequestHeader('Cache-Control', 'no-cache');

if (start_func !== undefined) {
    start_func.apply();
    setTimeout(function() {
        req.send(frm_data);
    }, 500);
} else {
    req.send(frm_data);
}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FormData in IE is only supported from IE10, after doing some research the best solution for this (my opinion) would be using ajaxForm: http://malsup.com/jquery/form/ works perfect in IE8/9, not sure about IE7


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...