使用symfony2並沒有利用它的好處,你正在做一個很大的錯誤,你應該適當地構建你的應用程序,但是至於你的問題,關於symfony沒有什麼限制,但你自己如何使用它。你可以得到Sonata媒體管理器服務,您必須手動設置媒體管理器所需的所有設置器,並且您必須手動處理文件大小,文件MIME類型等驗證。下面是演示如何將文件存儲在索納塔媒體捆綁
/* Before class use these*/
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Application\Sonata\MediaBundle\Entity\Media;
public function uploadAction()
{
$file = $this->get('request')->files->get('filefieldname');
if (!$file instanceof UploadedFile || !$file->isValid()) {
return new Response(json_encode(array(
'event' => 'uploader:error',
'data' => array(
'message' => 'Missing file.',
),
)));
}
/* validate max min size for the file */
/* validate mime type for the file */
/* Get sonata media manager service from container */
$mediaManager = $this->container->get('sonata.media.manager.media');
/* create new instance of sonata media class in my case entity is located at
* Application\Sonata\MediaBundle\Entity\Media
*/
$media = new Media();
$media->setBinaryContent($file);
$media->setContext('default');
$ImagemimeTypes = array('image/jpeg', 'image/png');
$FilemimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/msword', 'application/pdf', 'application/x-pdf');
if (in_array($file->getMimeType(), $FilemimeTypes)) {
$media->setProviderName('sonata.media.provider.file');
}
if (in_array($file->getMimeType(), $ImagemimeTypes)) {
$media->setProviderName('sonata.media.provider.image');
}
/*If you have other providers in your app like vimeo /dailymotion etc
then do set here i have shown you the demo for adding image/file in sonata media */
/*other setters if you want to set like enabled/disable etc*/
$mediaManager->save($media);
// end function
}
但再次會有很多返工你必須做它的symfony已經爲您提供了方便的
也許你應該看看:https://gist.github.com/romaricdrigon/6291435。這使您可以使用Symfony的方式與AngularJS結合構建表單。您可以以任何方式構建表單並將其從後端返回到SPA。 –