2
當我嘗試發送FormData對象與Ajax的PHP代碼沒有看到我的文件。 陣列$_FILES
是空的,也是$_POST
。 但在瀏覽器的ajax請求發送這個文件。PHP看不到來自javascript的數據ajax FormData(輸入類型文件)
<input type="file" name="fileToUpload" id="fileToUpload" value=""
<input id="uploadxml" class="upload-excel button" type="submit" value="Upload Excel File"
name="button">
$submit.on('click', function(e) {
e.preventDefault();
var formData = new FormData();
var xhr = new XMLHttpRequest();
formData.append('file', jQuery('#fileToUpload').prop('files')[0]);
xhr.open('POST', 'url', true);
xhr.send(formData);
xhr.onreadystatechange = function() {
if(xhr.status !== 200) {
console.log(xhr.status + ': ' + xhr.statusText);
} else {
if(xhr.readyState == 4) {
console.log(xhr.responseText);
}
}
};
});
<?php
echo $_POST;
echo $_FILES['file'];
?>
你看了在瀏覽器的控制檯中的AJAX請求/響應的數據?你在第一次輸入時是否忘記了大括號,還是僅僅是複製/麪食錯誤?你如何填充'$ submit'?爲什麼在使用jQuery語法開始請求時切換到VanillaJS?你從哪裏得到這個代碼? –
是的,我看過Ajax請求 ------ WebKitFormBoundaryh24pn8WrfIZtfseH Content-Disposition:form-data; NAME = 「文件」; filename =「DB-SampleForWritingScript.xlsx」 Content-Type:application/octet-stream ------ WebKitFormBoundaryh24pn8WrfIZtfseH-- – pmnazar
Brace是複製/粘貼錯誤。 這是我的代碼) – pmnazar