繁体   English   中英

使用 fetch() 在服务器上发送.zip 文件时遇到问题

[英]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) 多,您将获得一个数组内容

  • [Name]:名称(来自浏览器,视为污点)
  • [type] => image/jpeg(文件类型)
  • [tmp_name] => /tmp/php/php1h4j1o (取决于您的配置设置,但用户无法控制,所以这没有被污染)
  • [错误] => UPLOAD_ERR_OK (= 0)
  • [大小] => 123(以字节为单位的大小)

所以你得到你的文件数据。

在这里你可以找到像我在这里使用的很好的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM