Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make FileUpload Object handle Multiple files in Symfony2?

I'm trying to make a form with a multiple file field. Since the docs are quite vague:

  • http://symfony.com/doc/current/reference/forms/types/file.html
  • http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

I've decided to use W3C FileReader API (Based in the doc urls below), to handle files from the client and manage the underliying data from the view to the entity. Currently supports drag&drop, metadata, and multiple selections on the client.

  • http://www.html5rocks.com/en/tutorials/file/dndfiles/
  • http://playground.html5rocks.com/#reading_file_metadata
  • https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

But I want to give the UploadedFile object one more chance, and the MAIN problem I have is that I can't make the file property in my entity (FileUpload type) to store more than one file data. My input looks like this:

<input type="file" id="upload_files" name="upload[files][]" required="required" multiple="multiple" />

In theory If I made the name to be an Array, the fileUpload should contain the files but it doesn't. Can UploadedFile object store multiple files data ? or just single ?

http://api.symfony.com/2.2/Symfony/Component/HttpFoundation/File/UploadedFile.html

Also tried to initialize (in the __construct of the entity) the $files property as an array and modify the setFiles() to store new array index's $this->files[] = $file; ... you know.

But then Symfony tells me that exception:

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of Symfony\Component\HttpFoundation\File\File.

I'm not familiarized with data transformers. And Can't figure it out how can be done now. Or If it could be really useful to get the UploadedFile Object with every file data.

In synthesis... With this given info, and the code below. Could anyone help me to get the FileUpload object the correct number of files and not just the last added ? Thank You

Made a repo on github: https://github.com/jeflopo/fileupload

For brevity, here are the relevant files:

  • The form: https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Form/Type/FileUploadType.php

  • The Entity: https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Entity/FileUpload.php

  • The controller (Just see the uploadAction): https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Controller/DemoController.php

  • The View: https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Resources/views/Demo/upload.html.twig

  • The JavaScript that handles the files on the client (Doesn't affect to files behaviour in the server): https://github.com/jeflopo/fileupload/blob/master/src/Acme/DemoBundle/Resources/public/js/upload.js

like image 319
Jeflopo Avatar asked Oct 21 '22 14:10

Jeflopo


1 Answers

I created a pull request which fixes the error!

What's left to do now is that you create unique filenames for the uploaded files and then use the move method on each file. If you don't move them the files won't get saved!

As I set mapped to false, your entity doesn't contain the files. You have to create an array with the filenames you just created to save the file paths.

like image 78
hacfi Avatar answered Oct 29 '22 04:10

hacfi