Bonsoir;
Je coince sur ce problème depuis le matin, je m'en sers de VichUploaderBundle pour gérer l'ulpload des fichiers...
Récemment, j'ai créé une action DownloadFile pour mon contrôleur "CoursController" qui manipule l'entité CoursFile :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
 
namespace BacUp\GeneralBundle\Entity;
 
use BacUp\GeneralBundle\Entity\Cours as BaseCours;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File ;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
 
 
/**
 * CoursFile
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BacUp\GeneralBundle\Entity\CoursFileRepository")
 * @Vich\Uploadable
 */
class CoursFile extends BaseCours
{
 
 
    /**
     * @var string
     *
     * @ORM\Column(name="name_file", type="string", length=255)
     */
    private $nameFile;
 
     /**
     * @Vich\UploadableField(mapping="files_cours", fileNameProperty="nameFile")
     * 
     * This is not a mapped field of entity metadata, just a simple property.
     * @Assert\File(
     *     maxSize = "10M",
     *     mimeTypes = {"application/pdf", "application/x-pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/msword","image/png","image/jpeg"},            
     *     mimeTypesMessage = "يرجي التحقق من نوع الملف"
     * )
     *
     * @var File $coursFile
     */
    private $coursFile;
 
     /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $cours
     */
    public function setCoursFile(File $cours = null)
    {
        $this->coursFile = $cours;
 
        if ($cours) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            //$this->setdateUpd(new \DateTime('now')) ;
        }
    }
 
     /**
     * @return File
     */
    public function getCoursFile()
    {
        return $this->coursFile;
    }
 
    /**
     * Set nameFile
     *
     * @param string $nameFile
     * @return CoursFile
     */
    public function setNameFile($nameFile)
    {
        $this->nameFile = $nameFile;
 
        return $this;
    }
 
    /**
     * Get nameFile
     *
     * @return string 
     */
    public function getNameFile()
    {
        return $this->nameFile;
    }
 
    public function __toString()
{
    return $this->name;
}
}
Et voilà mon action Download:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public function downloadFileAction($id)
    {
        $em = $this->getDoctrine()->getManager();
 
        $cours = $em->getRepository('GeneralBundle:Cours')->find($id);
       /* var_dump($cours);
        die();*/
       // echo $entity->getTypeCours(); die();
       if(false === $this->get('security.context')->isGranted('ROLE_ADMIN'))
        {
            if(!$cours || $cours->getVal() == 0){
                throw $this->createNotFoundException('هذا الدرس غير موجود.');
            }
       }
 
        $entity = $cours;
        $helper = $this->container->get('vich_uploader.templating.helper.uploader_helper');
         //var_dump($helper); die();
        // C'est là que ça pose problème !?
        $path = $helper->asset($entity, 'files_cours');
 
 
        $path= $this->get('kernel')->getRootDir() . '/../web'.$path; 
        // check if file exists
        if (!file_exists($path)) {
            throw $this->createNotFoundException();
        }
 
        // prepare BinaryFileResponse
        $response = new BinaryFileResponse($path);
        $response->trustXSendfileTypeHeader();
        $response->setContentDisposition(
            ResponseHeaderBag::DISPOSITION_INLINE,
            $cours->getNameFile(),
            iconv('UTF-8', 'ASCII//TRANSLIT', $cours->getNameFile())
        );
 
        return $response;
    }
Et le outuput de cette action est décevant pour moi :
Mapping not found for field "files_cours"
500 Internal Server Error - MappingNotFoundException
Je vous invite aussi à voir la config que je mets pour ce Bundle:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Vich File Uploader Config    
vich_uploader:
    db_driver: orm
    mappings:
        files_cours:
            uri_prefix:         /files/cours
            upload_destination: %kernel.root_dir%/../web/files/cours
            namer:              vich_uploader.namer_origname
 
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        files_exos:
            uri_prefix:         /files/exos
            upload_destination: %kernel.root_dir%/../web/files/exos
            namer:              vich_uploader.namer_origname
 
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        files_solexos:
            uri_prefix:         /files/solexos
            upload_destination: %kernel.root_dir%/../web/files/solexos
            namer:              vich_uploader.namer_origname
 
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        files_exams:
            uri_prefix:         /files/exams
            upload_destination: %kernel.root_dir%/../web/files/cours
            namer:              vich_uploader.namer_origname
 
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        files_solexams:
            uri_prefix:         /files/solexams
            upload_destination: %kernel.root_dir%/../web/files/cours
            namer:              vich_uploader.namer_origname
 
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
Merci bien de me répondre