【问题标题】:How to post a binary file to the Files Service of the Dreamfactory API如何将二进制文件发布到 Dreamfactory API 的文件服务
【发布时间】:2017-02-03 05:51:27
【问题描述】:

在 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 调用中?提前感谢您的帮助!

【问题讨论】:

    标签: api upload binary multipartform-data dreamfactory


    【解决方案1】:

    我已经找到了解决方案。请提供任何改进的提示或建议,我是新手,但在过去 2 天里一直在研究和解决这个问题。我有以下设置:


    在客户端:

    发帖网址:/tableService/_table/tableName

    发布数据: 表单方法:multipart/form-data -您必须以key:value 对的形式提供每个表格字段 - 您必须为上传文件提供密钥“files” -您还需要您的身份验证标头(X-DreamFactory-Session-Token & X-Dreamfactory-API-Key


    在服务器上:

    您需要为表格设置预处理和后处理脚本。

    ...pre_process

    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 脚本中处理上传文件。

    ...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);
    

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2021-05-31
      相关资源
      最近更新 更多