javascript - 如何在没有 jQuery、iframes 或 flash 的情况下为 IE7/8 制作 AJAX 文件 uploader ?

标签 javascript php ajax

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

谁能给我解决方案如何在不使用 jQuery、iframes 或 flash 的情况下为 IE7/8 制作 AJAX 文件 uploader ?

这是我的 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. 否则使用您不想使用的上述选项之一

关于javascript - 如何在没有 jQuery、iframes 或 flash 的情况下为 IE7/8 制作 AJAX 文件 uploader ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333282/

相关文章:

javascript - 没有 Node 的常规 Web 应用程序中的 Jade Lang

php - 从程序中将数据保存或存储在数据库中

java - 免费的 Java HTML 和 JS 解析器

javascript - 如何使用 jQuery 将选定的文件传递给 php 脚本?

javascript - 为什么我的 jQuery 函数在 IE 8 中导致 'Stack Overflow' 错误?

javascript - 对使用按钮 OnClick 从带有表格的按钮调用 Ajax 感到困惑?

PHP 堆栈偶尔显示空白页

php - 如何使用mysql中的属性获取所有类别及其产品总和

javascript - 根据用户输入创建编号表单

jquery - MVC WebGrid 在其表格之前呈现 AJaX 脚本。如何在脚本部分强制生成脚本