我在 REST Api 中工作,我正在发送(上传)一个POST
. 我正在使用 Symfony 2.6.8 和 FOSRestBundle,我这样做是在方法上:
/**
* Set and upload avatar for reps.
*
* @param ParamFetcher $paramFetcher
* @param Request $request
*
* @ApiDoc(
* resource = true,
* https = true,
* description = "Set and upload avatar for reps.",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when errors"
* }
* )
*
* @RequestParam(name="rid", nullable=false, strict=true, requirements="\d+", description="The ID of the representative")
* @RequestParam(name="avatar", nullable=false, description="The avatar file")
*
* @return View
*/
public function postRepsAvatarAction(ParamFetcher $paramFetcher, Request $request)
{
$view = View::create();
$content = $request->getContent();
print_r($content);
$view->setData(array())->setStatusCode(200);
return $view;
}
这就是我得到的回应:
------WebKitFormBoundary3JCHTJWwvnOcvnSv
Content-Disposition: form-data; name="_format"
json
------WebKitFormBoundary3JCHTJWwvnOcvnSv
Content-Disposition: form-data; name="rid"
1
------WebKitFormBoundary3JCHTJWwvnOcvnSv
Content-Disposition: form-data; name="avatar"; filename="carlingford.jpg"
Content-Type: image/jpeg
/* Here goes a lot of code that I suppose is image or so in base64 - below is just a example*/
���p��
��T!1"AQa2q�#B�� R�$3b�C���%r�4S�&�5c
Ds��'ET�d6U��� ��L!1AQaq"��2����#��BR�b$3r����C%4S��s�&5DTc���?�g������F΅%v9��Ge�9�С@#B�'�y�4!p|hPW%<�8�:n��9�hI�B;�Ж�:R{�8Ћ(�m�:m�*����0��vƄ#��?'B���BQ����BGo}
;#�a#�΄U��W�x?�E��H��:��#Ռ�t�L�6C�u ��#�R����22����n�c���B�@3ƅ"��ZRN�dv΄�����Д��pB��'B]�rҥ�����Q���)P�� �R�����R9Y�I&��� =���X?�bI�u��ĮQ���!C�'dn�}��-n�s�j8 �c$����Q����$�q��� �@�J9$ryԁj�pN�9���B8+*��v�Sih�J}[�W��ґ���R�r�?��h�I�J!�P�?J��+��T���+��Q�)�m
od
如何捕获该 POST 文件并创建一个文件以通过 Symfony 上传到服务器?任何人都可以给我想法或示例代码吗?
编辑1:
由于我正在使用FOSRestBundle
并且NelmioApiDocBundle
我正在使用 Nelmio 沙盒模式,如下图所示: