jquery 的ajax無刷新上傳文件之后,頁面還是會莫名的刷新-----解決辦法


文件上傳用到全局數組: $_FILES

 

只需要把下面的 <button onclick="post()">提交</button> 改為 <input type="button" onclick="post()" value="提交"/>就不會刷新頁面了!!!

參考 http://bbs.csdn.net/topics/391852021

 

what fuck ... sb html   我在那愣是提交了半個小時,還是一直刷新,找不到問題所在。 原因是我的提交按鈕用的是button標簽,后來改成了 <input type="button"> 就尼瑪可以了   真是無語了

 

file.html

<html>
<head>
<meta content="" charset="UTF-8"/>
 <script src="./jquery.js"></script>
 </head>
 <form enctype="multipart/form-data" enctype="multipart/form-data">
     請選擇文件<input type="file" name="file"/>
     <button onclick="post();">提交</button>
 </form>
 <script>
    function post() {
        alert("in post");
        var formData = new FormData();
        //var formData = new FormData($("form")[0]);
        formData.append('file', $('input[name=file]')[0].files[0]);
        $.ajax({
            url: './file.php',
            type: 'POST',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            async: true 
        }).success(function(){
            alert("success!");
            }).error(function(){
                alert("error");
                }); 
            
    }   

</script>
</html>

  

file.php

<?php
file_put_contents("./a.txt", var_export($_FILES, true));
//var_dump($_FILES);exit;


if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "text/plain"))
        && ($_FILES["file"]["size"] < 20000))
{

    file_put_contents("./a.txt", "\n in \n", FILE_APPEND);
    if ($_FILES["file"]["error"] > 0)
    {   
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }   
    else
    {   

        file_put_contents("./a.txt", "\n in else\n", FILE_APPEND);
/*        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 */

        if (file_exists( $_FILES["file"]["name"]))
        {   
   //         echo $_FILES["file"]["name"] . " already exists. ";
            file_put_contents("./a.txt", "\n already exists \n", FILE_APPEND);
        }   
          else
        {   
            file_put_contents("./a.txt", "\n in create file \n", FILE_APPEND);
            move_uploaded_file($_FILES["file"]["tmp_name"],
                                $_FILES["file"]["name"]);
     //       echo "Stored in: " . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}
?>

  

 

這樣的結果才是正確的,頁面 /file.html 沒有刷新, 解決辦法是submit 按鈕改成了

<input type="button" value="提交"/>

 

---------------------------------

而之前的按鈕是 <button>提交</button>,這樣文件上傳成功之后是會刷新頁面的,去請求 /file.html?file=file.txt 這個路徑的文件,很是奇怪!!!

點擊提交

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM