0

I am uploading images using ajax and php. My code is working fine in firefox. But in I.E, it doesn't work!

Here is my HTML code,

<!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>

Here is php code,

<?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!";
}
?>

In firefox the file uploads perfectly and alert comes up, But in I.E nothing happens!

4

1 回答 1

3

从表单插件的示例页面

支持 XMLHttpRequest Level 2 的浏览器将能够无缝上传文件。

IE 不支持 XMLHttpRequest Level 2。

编辑:

好的,这似乎不是 Ajax 问题,因为该插件使用 iframe 后备。您可能需要重构您的 javascript 代码

$(function(){
    $('form').ajaxForm({
        complete: function(xhr) {
            alert(xhr.responseText);
        }
    }); 

    $('#inp').change(function(e) {
        $('#btn').click();
    });
});

但附带说明一下,IE 中也没有文件拖放功能。因此,只有在 IE 中手动选择文件时它才有效。隐藏文件选择将不允许用户选择文件。在文件输入上从 javascript 引发点击事件也是不可能的,您必须使用透明的文件输入。

于 2012-08-06T11:25:42.830 回答