如何通过 API 发送和接收文件。我想通过 APi 发送和接收 pdf 和图像文件。我怎样才能这样做呢?例如,用户使用 python 发送文件(使用 python 进行 API 调用),我在端点(使用 PHP)接收该文件并将其保存到文件系统。如何通过 API 发送和接收文件?
API端点仅接受application/json
当有人通过 API(用 python 编写)向我发送文件时,我如何使用 PHP 处理该文件?
这是我迄今为止的解决方案:
在python端对文件进行base64编码 然后在PHP端对文件进行base64解码。
问题是我不知道如何通过 base64 解码来检索文件。
有人知道如何对文件进行 base64 解码并将其内容保存到数据库吗?
最佳答案
您必须使用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
});
关于php - 通过 API 发送(使用 Python)和接收(使用 PHP)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894695/