1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FOSRestBundleを使った場合の複数ファイルアップロード

Last updated at Posted at 2016-09-27

Symfony2でFOSRestBundleを使ったAPIサーバに、
複数ファイルをアップロードする処理を実装したときに、
ハマったのでメモを残します。

環境

  • Symfony2.8
  • FOSRestBundle2.1

1. 前提

今回のケースはParamFetcherを利用して、
送信されたファイルを取得するケースです。

ParamFetcherを利用する場合以下の設定がapp/config/config.ymlに必要です。

config.yml
fos_rest:
    param_fetcher_listener: true

2. Controllerの実装

Controllerは以下のように実装します。

HogeController.php
    /**
     * アップロード
     *
     * @Rest\Post("", name="hoge_video_upload")
     * @Rest\FileParam(name="files", map=true)
     */
    public function uploadAction(ParamFetcher $paramFetcher)
    {
        $files = $paramFetcher->get('files');

        $uploadManager = $this->get('hoge.upload_manager');
        $entities = $uploadManager->tryUpload($files);

        return $entities;
    }

FileParamに map=true オプションをつけるのがポイントで、
これを記載しないと複数のファイルをコントローラで受け取ることができません。

もともとFileParamで複数ファイルを取得できないバグがあったようですが、
つい最近(2016/08)、Ver2.1で修正されたようです。
それまでは複数ファイルの取得は、$request->files->get('image-data') で取得するしかなかったようです。

3. 参考

https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1474
https://github.com/FriendsOfSymfony/FOSRestBundle/issues/1535

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?