2

I would like to use the OneUploader bundle in a Symfony2 form that also contains other fields. In other words, the uploaded file is handled as an attachment to the rest of the data in the form. But I can only find instructions on how add the uploader as a separate form with it's own controller to which the files are sent. So how do I handle the use case I just described?

Jens Wegar
  • 4,147
  • 4
  • 29
  • 34

1 Answers1

3

Bundle dev here.

Frontend uploaders like jQuery File Upload or Dropzone always forge a seperate request when uploading a file to the server. This means that the upload process takes place before the form handling in your controller. If you really want to upload file along with the main form request, then you should not use such an uploader. Instead, create an Entity Media (or the like) map it to the base entity with a OneToMany or ManyToMany association and add it to the form with an Entity type.

There are some pretty good answers here on StackOverflow, for example this one.

However, if you choose to not use a frontend uploader all your files will be sent to the server simultaneously. Depending on the file size / server configuration this can result in upload errors. Moreover the upload will not be performed asynchronously anymore, which forces the user to wait for the upload to complete, after he submitted the form.

There is general problem when dealing with asynchronous multiple uploads on creation forms. I tried to answer a similiar question here.

The problem with enabling file uploads on the create mask is that you eventually end up with orphaned files. This is because a user is able to trigger the upload without saving the actual entity.

Your problem seems like a good use case for the Orphanage feature of this bundle. It lets you upload files before an actual entity exists to attach these files to. After the main form has been submitted you can retrieve the files and perform some more logic on top of it.

Note: This is by no means a perfect solution. Take a look at the limitations! Summarized can be said; as this feature is based on the session, a user may end up having uploaded a file twice. Be sure to handle this accordingly.


And then there is the possibility that you just want to add some more data to the file upload request: As this is handled in the frontend uploader, it differs from implementation to implementation. For example the jQuery File Uploader just serializes the whole form and sends along all other values, including hidden fields.


Personal Recommendation: I'd not send the file along with the form submit request. Instead use a frontend uploader and either:

  • Let users upload files not until the entity exists where it should be attached. This is a quite common strategy and in most cases the desired one. A simple second step when creating an entity should be sufficient.
  • Take a look at the Orphanage feature if you really want to be able to upload files directly on the creation mask.
Community
  • 1
  • 1
devsheeep
  • 2,076
  • 1
  • 22
  • 31
  • Thank you for this detailed answer. I'll need to do a bit of thinking to see if it makes sense to change the form so that uploading files is possible only after the entity was created, or if the Orphanage feature is a better option. Either way I agree that one should avoid sending especially large files with the form request. – Jens Wegar Mar 25 '14 at 13:01