上传使用JavaScript创建的Blob文件到PHP服务器。

huangapple go评论159阅读模式
英文:

Upload a blob file created using javascript to a php server

问题

JavaScript:

  1. let myFile = new Blob(["A file"], { type: "text/plain" });
  2. fetch("server_location.php", {
  3. method: "POST",
  4. body: myFile,
  5. headers: {
  6. "Content-Type": "text/plain"
  7. })
  8. .then((res) => { return res.text(); })
  9. .then((text) => { console.log(text); });

PHP:

  1. <?php
  2. var_dump($_FILES);

Required
应该在文件数组中显示文件
Output
array (0)

请提供一个解决方案,以便我可以将在JavaScript中创建的自定义文本Blob文件上传到我的PHP服务器。

英文:

Javascript:

  1. let myFile = new Blob([&quot;A file&quot;], { type: &quot;text/plain&quot;});
  2. fetch(&quot;server_location.php&quot;, {
  3. method: &quot;POST&quot;,
  4. body: myFile,
  5. headers: {
  6. &quot;Content-Type&quot;: &quot;text/plain&quot;
  7. })
  8. .then((res) =&gt; { return res.text(); }
  9. .then((text) =&gt; { console.log(text) };

Php:

  1. &lt;?php
  2. var_dump($_FILES)

Required
Should show the file in the file array
Output
array (0)

Please give me a solution so that I can upload custom text blob file created in javascript to my php server.

答案1

得分: 2

$_FILES 是在上传一个以多部分表单数据格式的请求时填充的,并且带有当前的内容类型标签。

您正在上传一个标记为纯文本的 blob,并进行字符串化(可能为 [object Object])。

要发送一个多部分请求,请使用 FormData 对象

  1. const myFile = new Blob(["A file"], { type: "text/plain" });
  2. const data = new FormData();
  3. data.append("myFile", myFile, "filename.txt");
  4. fetch("server_location.php", {
  5. method: "POST",
  6. body: data,
  7. })

要发送纯文本并进行读取:

  • 不要使用 blob
  • 从 STDIN 读取
  1. fetch("server_location.php", {
  2. method: "POST",
  3. body: "a plain text string",
  4. headers: { "Content-Type": "text/plain" }
  5. })

以及

  1. $data = file_get_contents("php://input");
英文:

$_FILES is populated when you upload a request formatted as Multipart Form Data and labeled with the current content-type.

You are uploading a blob labelled as plain text, and stringifed (probably to [object Object]).


To send a multipart request, use a FormData object.

  1. const myFile = new Blob([&quot;A file&quot;], { type: &quot;text/plain&quot;});
  2. const data = new FormData();
  3. data.append(&quot;myFile&quot;, myFile, &quot;filename.txt&quot;);
  4. fetch(&quot;server_location.php&quot;, {
  5. method: &quot;POST&quot;,
  6. body: data,
  7. })

To send plain text and read it:

  • Don't use a blob
  • Do read from STDIN
  1. fetch(&quot;server_location.php&quot;, {
  2. method: &quot;POST&quot;,
  3. body: &quot;a plain text string&quot;,
  4. headers: { &quot;Content-Type&quot;: &quot;text/plain&quot; }
  5. })

and

  1. $data = file_get_contents(&quot;php://input&quot;);

答案2

得分: -4

如果您正在发送文件,则“Content-Type”不应为“text/plain”。

将Content-Type设置为“multipart/form-data”。

{
"Content-Type": 'multipart/form-data'
}

英文:

If you are sending a file then "Content-Type" shouldn't be "text/plain".

Set Content-Type to "multipart/form-data".

  1. {
  2. &quot;Content-Type&quot;: &#39;multipart/form-data&#39;
  3. }

huangapple
  • 本文由 发表于 2023年3月7日 18:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定