dth42345 2014-07-07 14:51
浏览 60

在链配置的命名空间异常中找不到Symfony2多文件上载

So I've been working on getting my website to upload multiple files at once, and I've gotten pretty close (I think), but I'm getting the following error "The class 'Symfony\Component\HttpFoundation\File\UploadedFile' was not found in the chain configured namespaces". I think it's because I'm uploading the file as

$data = $form->getData();
$files = $data['file'];

foreach($files as $file)
{
    $photo = new Photo();
    $photo->setFile($file);
    $em->persist($file);
}

instead of connecting $photo to the createFormBuilder, if that makes any sense. I'm not sure what to do here, since I don't want to loose the auto_mapping from doctrine. Is there another workaround here, to make doctrine map UploadedFile? Here's my full code

Controller

Entity

Photo Entity YML

  • 写回答

2条回答 默认 最新

  • dongyuruan2957 2014-07-07 15:34
    关注

    You cannot do this. Doctrine tries to persist UploadedFile class and it is not entity class at all.

    Possible workarounds:

    • Save the filename
    • Save the file content (BLOB data)

    Generally, the easier way is to save file name only, BUT, you also need to move file, since the file is being uploaded to /tmp by default. Failing to move it will result in losing the file altogether.

    UploadedFile comes with useful method move which you could use to achieve file relocation.

    Another solution (less code but far more complex):

    About a week ago I released my own bundle that handles file uploads. You can find it here: https://github.com/jperovic/exploring-fileutility

    It tackles with this issue by moving the file and giving you the file name so you could write it to the database

    Disclaimer: it is not my intention to hijack you question with bundle promotion. Just say the word and I will remove it from answer.

    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”