[英]Trouble with sending .zip file on server with fetch()
我嘗試通過這種方式在服務器上發送 my.zip 文件:
function sendData(){
let zipFile = document.getElementById('fileReciever').files[0];
let formData = new FormData();
formData.append('id', btoa('7804044924'));
formData.append('data', zipFile);
fetch('http://localhost/xmlReader/reciever.php', {method: "POST", body: formData});
}
但是在 $_POST 的服務器(php)上,我只得到“id”字段,“data”不存在。 我做錯了什么?
我嘗試這種方式:
let zipFile = document.getElementById('fileReciever').files[0];
let formData = new FormData();
formData.append('id', btoa('7804044924'));
formData.append('data', zipFile);
//fetch('http://localhost/xmlReader/reciever.php', {method: "POST", body: formData});
let req = new XMLHttpRequest();
req.open("POST", 'http://localhost/xmlReader/reciever.php');
req.send(formData);
但結果是相同的 - 在 $_POST 的服務器上我只得到一個字段 - “id”,字段“data”不存在。
如果要捕獲文件,則必須使用 $_FILES 而不是 $_POST。
假設您發送的圖像比您的 var_dump($_FILES) 多,您將獲得一個數組內容
所以你得到你的文件數據。
在這里你可以找到像我在這里使用的很好的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.