首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用angular + symfony + vichuploaderBundle上传图片

如何使用angular + symfony + vichuploaderBundle上传图片
EN

Stack Overflow用户
提问于 2015-03-17 01:29:22
回答 1查看 988关注 0票数 5

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

想法如下,

我有一些标签,如果你点击它们,它们会显示不同的表单,其中一个是用于文件上传的。我的问题是,我如何上传图像?我的意思是正确的方式。我有一个html.twig文件,里面有一个表单(我使用的是includes of twig engine)。假设我有这个form.html.twig

代码语言:javascript
复制
 <form onsubmit="{{ path('upload-new-file') }}">
   <input type="file" id="someFile"/>
        <button> Upload Image </button>
 </form>

一旦你选择了图片,点击Upload,这将决定哪个网址与upload-new-file(routing.yml)匹配(例如,它将执行一些查询来上传文件)

我的主要问题是我被弄糊涂了,因为我一直在用php编程我的表单(使用createForm,form-> is isvalid等),然后用twig呈现它们,我也使用vichUploaderBundle。在我描述的情况下,我无法做到这一点,因为我没有“表单”来呈现它。({{form(form)}})。我没有以通常的方式将表单作为参数传递(就像symfony文档中一样;$this->render(‘omeTemplate.html.twig’,array (' form‘=> $form)

假设我们有一个带有选项卡的网页,如果您单击其中一个选项卡,它将显示某个表单,其中一个表单包含上载输入,您选择一个图像并单击upload,然后呢?回想一下,我使用angularjs、vichuploaderbundle、symfony和Doctrine作为ORM。

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2015-08-16 19:24:30

twig文件

代码语言:javascript
复制
{{ 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)}}

类上载器

代码语言:javascript
复制
<?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;
    }

}

实体类

代码语言:javascript
复制
class entity extends Uploder
{

 protected function getUploadDir()
    {
         return 'dirctory name';
    }
}

并添加文件路径,然后删除路径

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29083255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档