dongyuan1983 2019-07-19 19:25
浏览 46

symfony 4无法上传图片FOSRestBundle

I am new to symfony4 and I am following youtube tutorial, to make todo list app with FOSRestBundle.

The problem I encounter is that I can't upload image to the server using PATCH method.

here is controller my function:

use FOS\RestBundle\Request\ParamFetcher;
/**
 * @Rest\FileParam(name="image", description="The background of the list", nullable=false, image=true)
 * @param ParamFetcher $paramFetcher
 * @param int $id
 * @return int
 */

public function backgroundListsAction(ParamFetcher $paramFetcher, int $id)
{
    var_dump($paramFetcher->get('image'));

    return count($_FILES);
}

here is bin/console debug:router log:

 ------------------ -------- -------- ------ ---------------------------- 
  Name               Method   Scheme   Host   Path                        
 ------------------ -------- -------- ------ ---------------------------- 
  background_lists   PATCH    ANY      ANY    /api/lists/{id}/background  
  get_lists          GET      ANY      ANY    /api/lists                  
  get_list           GET      ANY      ANY    /api/lists/{id}             
  post_lists         POST     ANY      ANY    /api/lists                  
  get_lists_tasks    GET      ANY      ANY    /api/lists/{id}/tasks       
  put_lists          PUT      ANY      ANY    /api/lists                  
 ------------------ -------- -------- ------ ---------------------------- 

and I tried several options:

1) As author of tutorial done it with curl, didn't work for me:

curl -X POST -F image=@/home/jake/Pobrane/matrixbg.jpeg -F method=PATCH http://localhost:8000/api/lists/1/background

I received an error:

{"code":405,"message":"No route found for \"POST \/api\/lists\/1\/background\": Method Not Allowed (Allow: PATCH)"}

2) I tried also curl this way:

curl -X PATCH -F image=@/home/jake/Pobrane/matrixbg.jpeg  http://localhost:8000/api/lists/1/background

Than error I got was:

{"code":400,"message":"Parameter \"image\" of value \"NULL\" violated a constraint \"This value should not be null.\""}

3) So last thing I tried was using postman, which ended up with same result as in second case

enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP