angularjs – 如何使用angular symfony vichuploaderBundle上传
我正在尝试使用angular和vichUploaderBundle为symfony上传图像.
这个想法如下, 我有一些标签,如果你点击它们,它们会显示不同的表格,其中一个用于文件上传. <form onsubmit="{{ path('upload-new-file') }}"> <input type="file" id="someFile"/> <button> Upload Image </button> </form> 选择图像后,单击上传,这将确定哪个URL与upload-new-file(routing.yml)匹配(例如,它将执行一些查询以上载文件) 我的主要问题是我感到困惑,因为我一直在使用php编程我的表单(使用createForm,form-> isvalid等),然后用twig渲染它们,我也使用vichUploaderBundle. 想象一下,我们有一个带有标签的网页,如果你点击其中一个标签,它会显示一些表格,其中一个表格包含一个上传输入,你选择一个图像并点击上传,那么呢?回想一下,我使用angularjs,vichuploaderbundle,symfony和Doctrine作为ORM. 提前致谢! 解决方法
树枝文件
{{ form_start(form,{'attr': {'novalidate': 'novalidate'} }) }} <div class="form-group"> <label>{{"news.lbl.file"|trans}}</label> {{form_widget(form.file)}} <lable>{{form_errors(form.file)}}</lable> </div> <div class="form-group"> {{form_widget(form.submit)}} </div> {{ form_end(form)}} 上课 <?php namespace BaseBundleEntity; use DoctrineORMMapping as ORM; use SymfonyComponentValidatorConstraints as Assert; use SymfonyComponentHttpFoundationFileUploadedFile; abstract class Uploader { /** * @var integer * * @ORMColumn(name="id",type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORMColumn(name="path",type="string",length=500,nullable=true) */ protected $path; /** * Set imageUrl * * @param string $path * @return Category1 */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Get id * * @return integer */ public function getId() { return $this->id; } 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($docroot="web") { // the absolute directory path where uploaded // documents should be saved return __DIR__ . "/../../../../$docroot/" . $this->getUploadDir(); } protected abstract function getUploadDir(); /** * @AssertFile(maxSize="6000000") */ protected $file; /** * Sets file. * * @param UploadedFile $file */ public function setFile(UploadedFile $file = null) { $this->file = $file; } /** * Get file. * * @return UploadedFile */ public function getFile() { return $this->file; } public function upload() { // the file property can be empty if the field is not required if (null === $this->getFile()) { return; } // use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the // target filename to move to $name = $this->getUploadDir() . "/" . time() . "-" . $this->getFile()->getClientOriginalName(); $this->getFile()->move( $this->getUploadRootDir(),$name ); // set the path property to the filename where you've saved the file $this->path = $name; // clean up the file property as you won't need it anymore $this->file = null; } } 实体类 class entity extends Uploder { protected function getUploadDir() { return 'dirctory name'; } } 并添加归档路径然后删除路径 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |