zoukankan      html  css  js  c++  java
  • ajax 异步 跨域上传图片

    客户端

    <label for="text">名称</label>
        <input type="text" id="text" name="name"/>
        <label for="file">文件</label>
        <input type="file" id="file" name="file"/>
        <button type="button" onclick="ajaxUploadFile()">确定</button>
        <script type="text/javascript">
            function ajaxUploadFile(){
                var formData = new FormData();
                var xmlhttp;
                if(window.XMLHttpRequest){//IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }else{
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("POST","http://upload.com/cors.php",true);
    //             xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
                formData.append("name",document.getElementById("text").value);
                formData.append("file",document.getElementById("file").files[0]);
                xmlhttp.send(formData);
                xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState==4){
                        if(xmlhttp.status==200){
                            console.log("上传成功"+xmlhttp.responseText);//abc
                        }else{
                            console.log("上传失败"+xmlhttp.responseText);
                        }
                    }
                }
            }
        </script>

    服务端

    header('Access-Control-Allow-Origin:*');//重要的跨域代码
    move_uploaded_file($_FILES['file']['tmp_name'],dirname(__FILE__).'/cors.png');
    echo 'abc';
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    shell 脚本编程学习
    LMH6505 vs AD8336
    ubuntu 8.04 NFS服务的配置(转)
    新博客开张了!
    基于ARMlinux环境下的音频系统开发
    可恶的英语考试
    转HashTable(C#)
    我的手机3300
    高效注册DLL控件 让你的IE浏览器复活
    学习.NET2.0随笔
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/8949980.html
Copyright © 2011-2022 走看看