1

在 Dreamfactory (v2.4.2) 中,我发布(通过 multipart/form-data)到 service/_table/{tablename} 以在表中创建记录。使用 PHP,然后我使用该记录的 ID 作为文件存储的文件夹名称返回。我想使用 platform.api.post 方法发布 service/_table/{tablename}.post_process 脚本并将原始请求中的二进制数据传递给它,但是我很难确定如何在没有base64_encoding 它。

如何将原始事件请求中的 multipart/form-data 请求传递到另一个服务的内部 API 调用中?在此先感谢您的帮助!

4

1 回答 1

0

从那以后,我找到了解决方案。请提供任何改进的提示或建议,我是相当新的,但在过去的 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);
于 2017-02-02T21:51:27.007 回答