afe_single_upload
Single Upload adds twitter-boostrap GUI wrapper for FileType.
- Select new file for upload
- Replace/Delete uploaded file
- Name uploaded/selected for upload file
- Uploaded image thumbnail preview / icon for other file types
- Selected image thumbnail preview / icon for other file types
- Uploaded file size
- Selected file size (browsers with HTML5 File API support only)
To use this form type you must choose one of the following methods:
Install VichUploaderBundle and configure it's mappings:
vich_uploader:
db_driver: orm
gaufrette: false
storage: vich_uploader.storage.file_system
mappings:
user_avatar:
uri_prefix: /user/avatar
upload_destination: %kernel.root_dir%/../web/user/avatar
namer: vich_uploader.namer_uniqid
inject_on_load: true
delete_on_remove: true
delete_on_update: true
Add Vich mappings to your entity:
<?php
// Acme\DemoBundle\Entity\User.php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @Vich\Uploadable
*/
class User
{
/**
* @Vich\UploadableField(mapping="user_avatar", fileNameProperty="avatar_path")
*/
protected $avatar;
/**
* (Optional) holds file name
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $avatar_name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $avatar_path;
}
Note: In this example we use Doctrine ORM, replace it's mappings with your ORM.
To use VichUploaderBundle you have to configure the upload_manager
option. Read more in
cookbook entry [How to use upload manager with this bundle?]
(https://github.com/avocode/FormExtensions/blob/master/Resources/doc/cookbook/upload-manager.md)
Follow symfony's cookbook on [How to handle File Uploads with Doctrine] (http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html) and apply following changes to your entity:
<?php
// Acme\DemoBundle\Entity\User.php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class User
{
protected $avatar;
public function getAvatar()
{
// inject file into property (if uploaded)
if ($this->getAbsolutePath()) {
return new \Symfony\Component\HttpFoundation\File\File(
$this->getAbsolutePath()
);
}
return null;
}
// see note below
public function getAvatarWebPath()
{
return $this->getWebPath();
}
}
Note: The name the second method must be
get FIELD WebPath
, in this example our field name isavatar
so method name isgetAvatarWebPath
.
Admingenerator basic configuration:
cover:
label: Avatar
formType: afe_single_upload
addFormOptions:
nameable: avatar_name
deleteable: avatar_path
data_class: Symfony\Component\HttpFoundation\File\File
type: string|boolean
default: false
If specified, uses this property to store file name.
type: string|boolean
default: false
If specified, will set this property to null
on delete action.
Note: The file will not be physically removed, unless you customize the setter. Hovever if you're useing VichUploaderBundle with
vich_uploader.namer_uniqid
namer then the file will be overwriten upon uploading another one.
type: integer
default: 320
Maximum preview image width. If image is bigger it will be scaled down to fit.
type: integer
default: 180
Maximum preview image height. If image is bigger it will be scaled down to fit.
type: integer
default: 16
Minimum preview image width. If image is smaller it will be scaled up to fit.
type: integer
default: `16
Minimum preview image height. If image is smaller it will be scaled up to fit.
type: boolean
default: true
If false, preview for images will not be displayed.
type: boolean
default: true
If true and supported by the browser, selected images will be previewed as canvas elements, otherwise img elements will be used.
type: string
default: null
If set, given filter will be applied on uploaded file to generate a thumbnail.
Note: Requires instaling and configureing image manipulator. See [How to use image manipulator with this bundle?] (https://github.com/avocode/FormExtensions/blob/master/Resources/doc/cookbook/image-manipulator.md) cookbook entry.
Special thanks to FileSquare for Filetypeicons.com
licensed under [Creative Commons Attribution-ShareAlike 3.0 Hong Kong License]
(http://creativecommons.org/licenses/by-sa/3.0/hk/).
This bundle uses icons created by loostro and
based upon Filetypeicons.com
. They are released under the same license as the
original work (you must credit FileSquare, you don't have to credit
loostro).