標籤:http net ... echo 頁面 解決 topic else sts
檔案上傳用到全域數組: $_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
<?phpfile_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 這個路徑的檔案,很是奇怪!!!
點擊提交
jquery 的ajax無重新整理上傳檔案之後,頁面還是會莫名的重新整理-----解決辦法