如何在没有jQuery或iframe或flash的情况下为IE7 / 8制作AJAX文件上传器


How to Make AJAX file uploader for IE7/8 without jQuery or iframes or flash?

我在网上搜索,发现很多jQuery和iframes的解决方案。

任何人都可以给我解决方案如何在没有jQuery或iframe或flash的情况下为IE7/8制作AJAX文件上传器?

这是我的 HTML 脚本

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'>
    <title>Ajax Uploader</title>
    <script>
    function iajax() 
    {
        oField=document.myform.myfile;
        if (window.XMLHttpRequest) 
        {
            xmlhttp=new XMLHttpRequest(); 
        } 
        else 
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            { 
                document.getElementById('eds').innerHTML=xmlhttp.responseText; 
            } 
        };
        xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");
        xmlhttp.open("post","file.php",true);
        xmlhttp.send();
    }
    </script>
</head>
<body>
    <form name='myform' enctype="multipart/form-data" method="post" onsubmit="iajax(this)">
        <input type="file" name="myfile" />
        <button type="submit">Upload file to the server</button>
    </form>
    <div id='eds'></div>
</body>
</html>

我的PHP脚本:

<?php
$name=$_FILES['myfile']['name'];
$type=$_FILES['myfile']['type'];
$size=$_FILES['myfile']['size'];
$temp_name=$_FILES['myfile']['tmp_name'];
if(!move_uploaded_file($_FILES['myfile']['tmp_name'], './public/'.$name))
{
    echo "Image Uploading Error";
}
else
{
    echo "Upload Image Name : ".$name;
}
?>
到目前为止,

我所知道的所有最新浏览器都是不可能的,如果您希望浏览器不刷新,则无法在没有 iframe 的情况下将文件发送到服务器,即使是众所周知的 js 框架和插件也使用 iframe。不确定闪光灯是如何做到的,但唯一的可能性是

  1. 要么不使用 ajax
  2. 否则使用您不想使用的上述选项之一