【问题标题】:Uploaded image Not Showing Symfony2上传的图片不显示 Symfony2
【发布时间】:2016-03-19 08:53:08
【问题描述】:

我使用 Symfony 网站文档提供的代码示例在 Symfony2 中创建文件上传功能

 public function getAbsolutePath()
{
    return null === $this->path
        ? null
        : $this->getUploadRootDir().'/'.$this->path;
}

public function getWebPath()
{
    return null === $this->path
        ? null
        : $this->getUploadDir().'/'.$this->path;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded
    // documents should be saved
   // return __DIR__.'/../../../../web/'.$this->getUploadDir();
    return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw up
    // when displaying uploaded doc/image in the view.
    return 'uploads/documents';
}

我可以上传文件但twig无法显示它

  <td><img src="/uploads/documents/{{ task.path }}" id="img-responsive">    </td>

使用 Google 的开发者工具检查显示图像的正确链接

<img src="/uploads/documents/Desert.jpg" id="img-responsive">

复制图片url到浏览器显示

   Object not found
   404

在这种情况下,您将如何显示在 Symfony 中上传的图像?

   app
   src
   web
     bundles
     uploads
       documents


     return __DIR__.'/../../../../../web/'.$this->getUploadDir();

更新

路径保存在数据库中

    path          id
    Desert.jpg     1
    cat.jpg        2
    dog.jpg        3

【问题讨论】:

  • 你真的应该使用 twigs {{ asset() }} 方法。然后在其中使用路径和文件名。

标签: symfony


【解决方案1】:

您可以使用 DevDonkey 建议,也可以像这样更改您的代码

 <td><img src="yourhost.com/web/uploads/documents/{{ task.path }}" id="img-responsive">    </td>

这应该可以工作,因为我尝试过并在我当前的项目中使用它..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2013-10-29
    • 2017-10-12
    相关资源
    最近更新 更多