【发布时间】:2012-08-03 08:44:35
【问题描述】:
我正在使用 ajax 和 php 上传图片。我的代码在 Firefox 中运行良好。但在 IE 中,它不起作用!
这是我的 HTML 代码,
<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
body { padding: 30px }
</style>
</head>
<body>
<h1>File Upload Progress Demo #1</h1>
<form action="fileup.php" method="post" enctype="multipart/form-data">
<input id="inp" type="file" name="uploadedfile" style="display:none"><br>
<input id="btn" type="submit" value="Upload File to Server" style="display:none">
</form>
<div id="fileSelect" class="drop-area">Select some files</div>
<script>
(function() {
$('form').ajaxForm({
complete: function(xhr) {
alert(xhr.responseText);
}
});
})();
var fileSelect = document.getElementById("fileSelect"),
fileElem = document.getElementById("inp");
fileElem.addEventListener("change",function(e){
document.getElementById('btn').click();
},false)
fileSelect.addEventListener("click", function (e) {
fileElem.click();
e.preventDefault();
}, false);
</script>
</body>
</html>
这里是php代码,
<?php
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
在 Firefox 中文件上传完美并出现警报,但在 IE 中没有任何反应!
【问题讨论】:
-
如果您使用 jQuery,我建议使用它来绑定事件,而不是 blah.addEventListener('click')... 你可以这样做 $('blah').on('click' , function(){}).....,让 jquery 为您处理该业务
-
我认为 Internet Explorer 不支持 XHR 上传。在 stackoverflow 上查看此响应。 stackoverflow.com/a/8899308/464064
-
如果你想跨浏览器上传ajax文件,你将不得不依赖一些基于flash的插件
-
只是想说明:github.com/malsup/form/#file-uploads 网站有一些关于支持的特定浏览器的信息,那么你怎么能说它必须与 Internet Explorer 一起使用呢?它是 MIT/GPL 许可的,没有任何保证,也不适合特定用途。如果你错过了什么,你必须添加它。这就是开源的工作原理。
标签: php html ajax internet-explorer file-upload