【问题标题】:Download few images with VichUploaderBundle使用 VichUploaderBundle 下载一些图像
【发布时间】:2015-06-21 22:23:49
【问题描述】:

我想用 VichUploaderBundle 上传文件。有我的实体

........
/**
 *
 * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
 *
 * @var File $imageFile
 */
protected $imageFile;

/**
 * @ORM\Column(type="string", length=255, name="image_name")
 *
 * @var string $imageName
 */
protected $imageName;

/**
 * @ORM\Column(type="datetime")
 *
 * @var \DateTime $updatedAt
 */
protected $updatedAt;

/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 */
public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        $this->updatedAt = new \DateTime('now');
    }
}

/**
 * @return File
 */
public function getImageFile()
{
    return $this->imageFile;
}

/**
 * @param string $imageName
 */
public function setImageName($imageName)
{
    $this->imageName = $imageName;
}

/**
 * @return string
 */
public function getImageName()
{
    return $this->imageName;
}
.........

我的表单生成器看起来像

  ........
  $builder
        ->add('brand')
        ->add('model')
        ->add('price')
        ->add('imageName')
        ->add('updatedAt')
        ->add('sub_rel')
 .......

我的配置文件看起来像

vich_uploader:
db_driver: orm
mappings:
    product_image:
        uri_prefix:         /images/goods
        upload_destination: %kernel.root_dir%/../web/images/goods

        inject_on_load:     false
        delete_on_update:   true
        delete_on_remove:   true

问题: 我需要更改哪些内容才能使用此捆绑包下载几张图片?

【问题讨论】:

  • 您能进一步解释一下吗?您想在一个视图中显示许多图像并有下载链接吗?您希望能够通过直接链接/api 下载它们吗?
  • @gvf 我想在管理面板中为我的商品上传图片
  • 我认为每个实例只能上传一张图片。您不能使用 vichuploader 处理同一属性的多个文件。不过我已经有一段时间没用了,也许改变了
  • 下载还是上传?

标签: php symfony vichuploaderbundle


【解决方案1】:

您的表单应如下所示:

->add('brand')
->add('model')
->add('price')
->add('image', 'vich_image', array(
    'required'      => false,
    'allow_delete'  => true, // not mandatory, default is true
    'download_link' => true, // not mandatory, default is true
));
->add('updatedAt')
->add('sub_rel')

如果你想在你的前端展示产品:

<img src="{{ vich_uploader_asset(product, 'image') }}" alt="{{ product.name }}" />

捆绑文档中都有详细记录:https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/index.md

我建议使用 LiipImagineBundle 为您的前端/后端生成所有需要的缩略图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多