douyabu1528 2017-08-05 14:42
浏览 40
已采纳

HTML阻止重定向上传php

I need to prevent the page redirected to the upload php when click upload button.

How can I do this in below code.

<form id="myForm"  action="http://example/DB_1/AccessWeb/file_upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload1">
</form>

<button onclick="myFunction()">  Upload
</button>

<script>

function myFunction(){

  document.getElementById("myForm").submit();


}
</script>
  • 写回答

3条回答 默认 最新

  • doukun5339 2017-08-05 16:01
    关注

    A very basic, quickly written example of how to send a file - using ajax to the same page so that the user doesn't get redirected. This is plain vanilla javascript rather than jQuery.

    The callback function can do more than print the response - it could, for instance, be used to update the DOM with new content based upon the success/failure of the upload.

    <?php
        $field='fileToUpload';
    
        if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_FILES ) ){
            $obj=(object)$_FILES[ $field ];
    
            $name=$obj->name;
            $tmp=$obj->tmp_name;
            $size=$obj->size;
            $error=$obj->error;
            $type=$obj->type;
    
            if( $error==UPLOAD_ERR_OK ){
                /* 
                    This is where you would process the uploaded file
                    with various tests to ensure the file is OK before
                    saving to disk.
    
                    What you send back to the user is up to you - it could
                    be json,text,html etc etc but here the ajax callback 
                    function simply receives the name of the file chosen.
                */
                echo $name;
            } else {
                echo "bad foo!";
            }
            exit();
        }
    ?>
    <!doctype html>
    <html>
        <head>
            <title>File Upload - using ajax</title>
            <script>
                document.addEventListener('DOMContentLoaded',function(e){
                    var bttn=document.getElementById('bttn');
                    bttn.onclick=function(e){
                        /* Assign a new FormData object using the buttons parent ( the form ) as the argument */
                        var data=new FormData( e.target.parentNode );
                        var xhr=new XMLHttpRequest();
                        xhr.onload=function(e){
                            document.getElementById('status').innerHTML=this.response;
                        }
                        xhr.onerror=function(e){
                            alert(e);
                        }
                        xhr.open('POST',location.href,true);
                        xhr.send(data);
                    };
                },false);
            </script>
        </head>
        <body>
            <form method='post' enctype='multipart/form-data'>
                Select image to upload:
                <input type='file' name='fileToUpload'>
                <input type='button' id='bttn' value='Upload' />
            </form><div id='status'></div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行