2015-10-01 60 views
1

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

的API端点只接受应用程序/ JSON

当有人向我通过API(Python编写)的文件,我怎么处理使用PHP文件?

这里的解决方案,我至今:

base64encode蟒结束 的文件,然后base64decode在PHP结束的文件。

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

有谁知道如何base64解码文件并将其内容保存到数据库?

+1

您是否尝试过使用 “move_uploaded_file()以” 并获得使用$ _ POST [ '文件']或$ _FILES [文件数据'文件']?你有没有得到任何错误? –

回答

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 
}); 
+0

如何从服务器端接受它? – Jasmine

+0

大概这将帮助你 http://stackoverflow.com/questions/17834243/how-to-read-formdata-object-in-php 我不知道,因为我不进入PHP –

+0

我试图在python中做这个而不是jquery – Jasmine

相关问题