从那以后,我找到了解决方案。请提供任何改进的提示或建议,我是相当新的,但在过去的 2 天里一直在研究和解决这个问题。我有以下设置:
在客户端:
发帖网址: /tableService/_table/tableName
发布数据:
表单方法:multipart/form-data
-您必须将每个表字段作为表单key:value
对提供-您必须为上传文件提供“ files
”键-您还需要您的身份验证标头(X-DreamFactory-Session-Token
& X-Dreamfactory-API-Key
)
在服务器上:
您需要为表设置预处理和后处理脚本。
...预处理
tableService._table.tableName.post.pre_process
if(!isset($_FILES['files']) || !is_array($_FILES['files'])){
// Stop execution and return a specific status code
$event['response']['errorCode'] = 400;
$event['response']['errorMessage'] = "File is missing. Check that the a file was provided and the field name is: [files], to continue.";
return false;
} else {
$event['payload'] = $_POST;
// use this next variable if your table has a field, "fileName" to store the file name. This should be changed to your needs
$event['payload']['fileName'] = $_FILES['files']['name'];
$event['payload'] = array("resource"=>array($event['payload']));
$event['request']['payload'] = $event['payload'];
$event['request']['content'] = json_encode($event['payload']);
$event['request']['content_type'] = 'json';
}
从这里开始,表格应该添加记录,我们将在post_process
表格的脚本中处理上传文件。
...后期处理
tableService._table.tableName.post.post_process
// we need to get the ID of the created record. Be sure to use the variable you want to use as your ID.
$recordId = $r['primaryKeyId'];
// For path, you can prepend an already existing directory if you need to like this.
$path = '/Documents/'.$recordId;
// The filename should be in the returned resource, assuming you store it. Otherwise, you can pull this from the $_FILES array, if you haven't changed it from the POST'd value. We should only have one file, so we'll use resource 0.
$filename = $event['payload']['resource'][0]['fileName'];
/**
* Prepping file for upload via API Endpoint
*/
// This is the URL for your internal files API Endpoint
$url = 'fileService/'.$path;
// let's create the directory, we need to make sure there is a trailing slash
$post($url.'/');
// we need to read the file data into a string, so we'll use file_get_contents on the tmp file.
$fileData = file_get_contents($_FILES['files']['tmp_name']);
$post($url.'/'.$filename, $fileData);