我想要做的是抓住一個上傳的圖像文件,並打印出$ _FILES數組來顯示圖像的不同屬性。我嘗試使用ajax,以便在提交表單時不需要重新刷新頁面。首先我用這行代碼在我的Ajax調用何時使用XMLHttpRequest的sentRequestHeader方法?
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
但我響應call.Then我註釋掉的代碼這一行收到一個空數組,我得到充滿了所有必要的信息數組關於上傳的圖像。
陣列(1){[ 「圖像」] =>陣列(5){[ 「名稱」] =>串(9) 「Koala.jpg」 [ 「型」] =>串(10 )「image/jpeg」[「tmp_name」] => string(24) 「C:\ xampp \ tmp \ php8E8B.tmp」[「error」] => int(0)[「size」] => int 780831)}}
所以我的問題時使用setRequestHeader()mehtod.When它必須在那裏,當它不能?謝謝!
<html>
<body>
<form id='form'>
<input type='button' value='submit' name='submit' id='btn'>
<input type='file' name='image' id='image'>
</form>
<span id='output'></span>
<script>
function callajax(e){
var form=document.getElementById('form');
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
document.getElementById('output').innerHTML=xhr.responseText;
}
}
var data=new FormData(form);
xhr.open("POST", "call.php", true)
// xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(data);
}
document.getElementById('btn').addEventListener('click',callajax,false);
</script>
</body>
</html>
PHP ::
var_dump($_FILES);
謝謝!如果它設置了不正確的MIME類型,那麼爲什麼我們需要它......以及發送標題的全部內容 –