【问题标题】:How to upload an image using angular + symfony + vichuploaderBundle如何使用 angular + symfony + vichuploaderBundle 上传图片
【发布时间】:2015-05-18 22:43:28
【问题描述】:

我正在尝试使用 angular 和 vichUploaderBundle 为 symfony 上传图像。

思路如下,

我有一些选项卡,如果您单击它们,它们会显示不同的表单,其中之一是用于文件上传。 我的问题是,如何上传图片?我的意思是正确的方法。 我有一个 html.twig 文件,里面有一个表单(我正在使用包含 twig 引擎)。 假设我有这个 form.html.twig

 <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。 在我所描述的情况下,我无法做到这一点,因为我没有“形式”来呈现它。 ({{表格(表格)}})。 我没有以通常的方式将表单作为参数传递(如在 symfony 文档中;$this->render('someTemplate.html.twig',array ('form' => $form)))

假设我们有一个带有标签的网页,如果你点击其中一个标签,它会显示一些表单,其中一个表单包含上传输入,你选择一张图片并点击上传,什么然后?回想一下,我使用 angularjs、vichuploaderbundle、symfony 和 Doctrine 作为 ORM。

提前致谢!

【问题讨论】:

    标签: angularjs forms symfony vichuploaderbundle


    【解决方案1】:

    树枝文件

    {{ 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 BaseBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
    
    abstract class Uploader
    {
    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(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();
    
        /**
         * @Assert\File(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';
        }
    }
    

    并添加归档路径然后删除路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多