gpt4 book ai didi

javascript - IE8/9 中的表单数据

转载 作者:行者123 更新时间:2023-12-02 22:49:11 26 4
gpt4 key购买 nike

我已经实现了这个脚本,用于使用ajax上传文件,它在除资源管理器之外的其他浏览器中运行完美,我注意到IE9及以下版本不支持formData,IE中是否有formData的替代品,我想使用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);
}}

最佳答案

IE 中的 FormData 仅受 IE10 支持,经过一些研究后,最佳解决方案(我认为)是使用 ajaxForm:http://malsup.com/jquery/form/在 IE8/9 中工作完美,不确定 IE7

关于javascript - IE8/9 中的表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15475215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com