我一直在尝试使用 VichUploader 在 Symfony 项目上上传文件,已经在使用 EasyAdmin?3。
我已正确配置所有内容,但出现此错误:
“pieceJointeFile”图像字段必须定义使用 setUploadDir() 方法上传图像的目录。
<?php
namespace App\Entity;
use App\Repository\InventaireRepository;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
My entity
/**
* @ORM\Entity(repositoryClass=InventaireRepository::class)
* @Vich\Uploadable
*/
class Inventaire
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Conducteur::class, inversedBy="inventaires")
*/
private $conducteur;
/**
* @ORM\Column(type="date")
*/
private $dateInventaire;
/**
* @ORM\Column(type="string", length=255)
*/
private $pieceJointe;
/**
* @Vich\UploadableField(mapping="pieceJointe", fileNameProperty="pieceJointe")
*/
private $pieceJointeFile;
/**
* @ORM\Column(type="datetime")
*/
private …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Api Platform 和 Vich Uploader Bundle 上传文件。当我发送带有 multipart/form-data 和实体 ID 的 POST 请求以附加图像文件时,我的实体收到 200 响应。但是上传的文件不会上传到目标目录,并且生成的文件名不会保留。没有错误,没有任何线索,不知道。
这是我的代码:
//vich uploader mappings
vich_uploader:
db_driver: orm
mappings:
logo:
uri_prefix: /logo
upload_destination: '%kernel.project_dir%/public/images/logo/'
namer: App\Infrastructure\Naming\LogoNamer
Run Code Online (Sandbox Code Playgroud)
//Organization Entity
<?php
namespace App\Infrastructure\Dto;
...use
/**
* @ORM\Entity()
* @ApiResource(
* iri="https://schema.org/Organization",
* shortName="Place",
* collectionOperations={
* "post" = {
* "denormalization_context" = {
* "groups"={
* "organization:collection:post"
* }
* }
* },
* "get" = {
* "normalization_context" = {
* "groups"={
* "organization:collection:get"
* } …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照文档使用API PLatform实现文件上传使用 Vich使用。但它不起作用,准确地说,MediaObject 没有被我根据我的请求发送的文件水合。
我完全遵循 API Platform 提供的说明书,但我的表单似乎没有很好地处理请求,因为它没有通过约束验证,我从 API 得到了这个答案:
{
"type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10",
"title": "An error occurred",
"detail": "file: This value should not be null.",
"violations": [
{
"propertyPath": "file",
"message": "This value should not be null."
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的要求是基本的:标头是 multipart/form-data,我发送一个小文件 (78Ko):"file" => image.jpg
当我转储 $request 对象时,文件在其中,但 $form->getData() 没有被水合。知道为什么吗?
这是我的 MediaObject :
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits\updatedAt;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Entity\File;
use Vich\UploaderBundle\Mapping\Annotation …Run Code Online (Sandbox Code Playgroud) 您有没有办法使用VichUploaderBundle重命名上传的文件?如果我上传两个具有相同名称的图像,它将删除旧图像,这对我来说是个问题.
我的配置与doc中的配置完全相同
如何在没有表格的情况下使用VichUploaderBundle上传文件?
我在某个目录中有文件(例如web/media/tmp/temp_file.jpg)
如果我试试这个:
$file = new UploadedFile($path, $filename);
$image = new Image();
$image->setImageFile($file);
$em->persist($image);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
由于未知错误,未上载文件"temp_file.jpg".
我需要从远程网址上传文件.所以我将文件上传到tmpdirecotry(使用curl),然后我一直尝试将它注入到VichUploadBundle中(如上所示).
当使用VichUploaderBundle上传任何文件时,此断言正在传递Symfony的表单验证:
/**
* @Vich\UploadableField(mapping="product_media", fileNameProperty="path")
* @Assert\File(
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "video/mp4", "video/quicktime", "video/avi"},
* mimeTypesMessage = "Wrong file type (jpg,gif,png,mp4,mov,avi)"
* )
* @var File $pathFile
*/
protected $pathFile;
Run Code Online (Sandbox Code Playgroud)
我看不出断言的问题.如何使用VichUploader验证文件类型?
我正在使用VichUploader在symfony项目中上传文件.在我使用的配置中(从文档中复制):
service: vich_uploader.namer_property
options: { property: 'slug'}
Run Code Online (Sandbox Code Playgroud)
在我的实体中,我使用Gedmo/Sluggable自动生成slu ::
/**
* @Gedmo\Slug(fields={"title"}, updatable=false)
* @ORM\Column(type="string", length=100, nullable=false)
*/
protected $slug;
Run Code Online (Sandbox Code Playgroud)
但是当试图保存实体时,我得到以下错误500:
无法生成文件名:属性slug为空.
如果我将属性设置为'title'就可以了.我是否忘记了配置参数或其他什么来使它与Gedmo slug一起工作?
我按照说明:https: //github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md写了这封信,但是在保存用户实体时出现以下错误:
Expected argument of type "AppBundle\Entity\File", "Symfony\Component\HttpFoundation\File\UploadedFile" given
Run Code Online (Sandbox Code Playgroud)
这是代码:
/**
* @ORM\Entity
* @ORM\Table(name="symfony_demo_user")
* @Vich\Uploadable
*/
class User extends BaseUser implements UserInterface
{
/**
* @Vich\UploadableField(mapping="avatar_image", fileNameProperty="avatarName")
*
* @var File
*/
private $avatarFile;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default": 0})
*
* @var string
*/
private $avatarName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return User
*/
public function setAvatarFile(File $image = null)
{ …Run Code Online (Sandbox Code Playgroud) 我将 Vich Uploader Bundle 与 API Platform 结合使用来存储文件。我已按照官方文档https://api-platform.com/docs/core/file-upload/#handling-file-upload中的说明进行操作
一切正常,除了我想包含 PUT 调用来替换现有的 MediaObject 实体。我的尝试如下:
我创建了一个自定义操作并将其映射到实体中的 PUT 方法,如下所示:
<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\CreateMediaObject;
use App\Controller\EditMediaObject;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ApiResource(
*
* ...
*
* itemOperations={
* "get",
* "put"={
* "controller"=EditMediaObject::class,
* "deserialize"=false,
* "validation_groups"={"Default", "media_object_create"},
* "swagger_context"={
* "consumes"={
* "multipart/form-data",
* },
* "parameters"={
* …Run Code Online (Sandbox Code Playgroud) 我希望你能帮助我,因为我正在寻找,但我迷路了:(
我正在尝试使用 VichUploaderBundle 在我的 symfony 6 项目中上传图像。
但我有这个错误:
类“App\Entity\Client”不可上传。
@Vich\Uploadable如果您使用注释来配置 VichUploaderBundle,您可能只是忘记在实体之上添加。如果您不使用注释,请检查配置文件是否位于正确的位置。在这两种情况下,清除缓存也可以解决问题。
我的 vich_uploader.yaml :
vich_uploader:
db_driver: orm
mappings:
clients_logo:
uri_prefix: '%client_logo%'
upload_destination: '%kernel.project_dir%/public%client_logo%'
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
Run Code Online (Sandbox Code Playgroud)
我的客户实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ClientRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\Common\Collections\ArrayCollection;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: ClientRepository::class)]
#[Vich\Uploadable]
class Client
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'integer')]
private $numero_client;
/**
* Some fields
*/
/**
* NOTE: This is not a mapped field of entity …Run Code Online (Sandbox Code Playgroud)