【问题标题】:send (using Python) and receive (using PHP) files via API通过 API 发送(使用 Python)和接收(使用 PHP)文件
【发布时间】:2015-10-01 18:45:16
【问题描述】:

如何通过 api 发送和接收文件。我想通过 APi 发送和接收 pdf 和图像文件。我该怎么做?例如,用户使用 python 发送文件(使用 python 进行 API 调用),我在端点(使用 PHP)接收它并将其保存到文件系统。如何通过 API 发送和接收文件?

API 端点只接受 application/json

当有人通过 API(用 python 编写)向我发送文件时,我如何使用 PHP 处理文件??

这是我目前的解决方案:

对python端的文件进行base64encode 然后在 PHP 端对文件进行 base64decode。

问题是我不知道如何通过base64解码来检索文件。

有谁知道如何对文件进行base64decode并将其内容保存到数据库中?

【问题讨论】:

  • 您是否尝试使用“move_uploaded_file()”并使用 $_POST['file'] 或 $_FILES['file'] 获取文件数据?你有什么错误吗?

标签: php jquery python api


【解决方案1】:

你必须使用FormData object

<form enctype="multipart/form-data" method="post" name="fileinfo">
  <label>Your email address:</label>
  <input type="email" autocomplete="on" autofocus name="userid" placeholder="email" required size="32" maxlength="64" /><br />
  <label>Custom file label:</label>
  <input type="text" name="filelabel" size="12" maxlength="32" /><br />
  <label>File to stash:</label>
  <input type="file" name="file" required />
  <input type="submit" value="Stash the file!" />
</form>
<div></div>

和js

var fd = new FormData(document.querySelector("form"));
fd.append("CustomField", "This is some extra data");
$.ajax({
  url: "stash.php",
  type: "POST",
  data: fd,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});

【讨论】:

猜你喜欢
  • 2012-10-11
  • 1970-01-01
  • 2016-06-16
  • 2021-03-14
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
相关资源
最近更新 更多