2015-08-04 22 views
5

VichUploaderBundle ile nasıl dosya oluşturabilirim?Symfony2: VichUploaderBundle ile manuel dosya yükleme

Bazı dizinde dosya bu denerseniz

(örneğin web/media/tmp/temp_file.jpg için):

$file = new UploadedFile($path, $filename); 

$image = new Image(); 
$image->setImageFile($file); 

$em->persist($image); 
$em->flush(); 

bende bu hatayı:

The file "temp_file.jpg" was not uploaded due to an unknown error.

ben dosya yüklemek için gerek uzak URL’den. Bu yüzden tmp direcotry (curl ile) dosyasına dosya yüklüyorum ve daha sonra onu (yukarıda gördüğünüz gibi) VichUploadBundle'a enjekte etmeye çalışıyorum.

cevap

2

Kısa yanıt: VichUploaderBundle, Symfony Form bileşenini kullanmadan dosya yüklemeyi desteklemiyor.

I need to upload file from remote url. So I upload file to tmp direcotry (with curl) and then I keep trying to inject it to VichUploadBundle (as you can see above).

Bu durumda, yüklemeniz gerekmez, yüklemeniz gerekir. Ve bu VichUploaderBundle'un ne anlama geldiği değil.

+0

Oh, bundan korkuyordum. Ama önemli değil, başka bir şey öğreneceğim. Çok teşekkür ederim! –

6

Kabul edilen yanıt doğru değil (artık?). usage documentation ile göre sen gerçekten elle Symfony'nin Formu Bileşeni kullanmadan bir File yükleyebilirsiniz:

/** 
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
* of 'UploadedFile' is injected into this setter to trigger the update. If this 
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
* must be able to accept an instance of 'File' as the bundle will inject one here 
* during Doctrine hydration. 
* 
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
*/ 
public function setImageFile(File $image = null) 
{ 
    $this->imageFile = $image; 

    if ($image) { 
     // It is required that at least one field changes if you are using doctrine 
     // otherwise the event listeners won't be called and the file is lost 
     $this->updatedAt = new \DateTime('now'); 
    } 
} 
1

Sen File yerine Symfony\Component\HttpFoundation\File\UploadedFile kullanmalıdır:

$file = new UploadedFile($filename, $filename, null, filesize($filename), false, true); 

VichUploaderBundle Bu nesneyi idare edecek.

+0

Bütünlük için, cevabı açıklayan cevabı ve/veya tercihen varsa github üzerindeki ilgili belgelere işaret eden bir bağlantı ekleyerek güncellemeniz daha iyi olacaktır (ya da değilse PR yapın). –