1

我是 Zend 的 Apigility 新手,文件上传有问题。如 Apigility 文档中所述,我在管理 UI 中创建了一个新的休息服务和配置字段:https ://apigility.org/documentation/recipes/upload-files-to-api

当试图从 InputFilter 获取任何数据时,我只得到空值。

资源控制器

public function create($data)
    {
        $inputFilter = $this->getInputFilter();
        $data = $inputFilter->getValues();
        var_dump($data);
        //return $this->attachments->create($data);
    }

var_dump 结果

array(1) {
  ["filedata"]=>
  NULL
}

出于测试目的,我使用了 Chrome 的 Postman 扩展,其中 Content-Type 标头设置为“multipart/form-data”,并将文件附加到密钥:filedata。

我很确定,我可以使用 json 和 base64_encode 发送文件,但我宁愿坚持下去,直到绝对必要为止。

4

3 回答 3

0

For those who aren't aware, Apigility is a Zend Framework 2 based framework specifically made for Rest/Rpc API's.

To do file uploads, please refer to their documentation on the recent updates as noted by Jon Day.

Credit : https://apigility.org/documentation/recipes/upload-files-to-api

How can you allow uploading files via your API?

Answer

Zend Framework 2 provides a variety of classes surrounding file upload functionality, including a set of validators (used to validate whether the file was uploaded, as well as whether it meets specific criteria such as file size, extension, MIME type, etc.), a set of filters (used to allow renaming an uploaded file, as well as, more rarely, to manipulate the contents of the file), and file-upload-specific inputs for input filters (because validation of files needs to follow different rules than regular data).

Currently the limitation is that Apigility will only accept multipart/form-data

Using Xdebug I am getting the following out :

$data_array = $inputFilter->getValues();
$image = $data_array['images_data'];

The $image array looks like this :

   name = MemeCenter_1400658513231_337.jpg
   type = image/jpeg
   tmp_name = /tmp/phpzV3mWA
   error = 0
   size = 379580
于 2014-06-10T20:12:45.353 回答
0

尝试这个

  1. 使用作曲家更新 apigility。文件上传在 1.0.3 版本中工作
  2. 使用邮递员发送文件但没有标题。只需选择表单数据。它对我有用。
  3. 要移动上传的文件,请使用重命名而不是 move_uploaded_file。
于 2014-08-14T19:35:04.873 回答
0

您可以将选项 ( 'magicFile' => false) 用于 MimeType 验证器,该验证器无需对 zf 库进行任何修改即可修复问题。

于 2014-12-18T04:52:34.567 回答