简体   繁体   English

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

[英]Trouble with sending .zip file on server with fetch()

I try to send my.zip file on server, this way:我尝试通过这种方式在服务器上发送 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});
}

but on server (php) in $_POST i getting only "id" field, "data" is absent.但是在 $_POST 的服务器(php)上,我只得到“id”字段,“data”不存在。 What i am doing wrong?我做错了什么?

i try this way:我尝试这种方式:

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);

but result is same - on server in $_POST i getting only one field - "id", field "data" is absent.但结果是相同的 - 在 $_POST 的服务器上我只得到一个字段 - “id”,字段“data”不存在。

If you want to catch the files you have to use $_FILES instead of $_POST.如果要捕获文件,则必须使用 $_FILES 而不是 $_POST。

Let's say you sent an image than you var_dump($_FILES), you will get an array content假设您发送的图像比您的 var_dump($_FILES) 多,您将获得一个数组内容

  • [Name]: the name (comes from the browser, so treat as tainted) [Name]:名称(来自浏览器,视为污点)
  • [type] => image/jpeg (type of the file) [type] => image/jpeg(文件类型)
  • [tmp_name] => /tmp/php/php1h4j1o (depending on your config settings, but the user has no control, so this isn't tainted) [tmp_name] => /tmp/php/php1h4j1o (取决于您的配置设置,但用户无法控制,所以这没有被污染)
  • [error] => UPLOAD_ERR_OK (= 0) [错误] => UPLOAD_ERR_OK (= 0)
  • [size] => 123 (the size in bytes) [大小] => 123(以字节为单位的大小)

and so you get your file-data.所以你得到你的文件数据。

Here you find good examples like the one i used here. 在这里你可以找到像我在这里使用的很好的例子。

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

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