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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: