[英]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.