Implementation of fully functional media uploading in web application

前端 未结 2 1695
醉酒成梦
醉酒成梦 2020-12-18 16:54

Suppose we have the web application which handle create, read, update and delete articles and each article should have gallery of images. I have to make one to one relation

2条回答
  •  悲哀的现实
    2020-12-18 17:34

    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. While creating a very own UploadBundle I thought about this problem for a while and came to the conclusion that there is no truly proper solution.

    I ended up implementing it like this:

    Given the fact that our problem arise from orphaned files, I created an Orphanage which is in charge of managing these files. Uploaded files will first be stored in a separate directory, along with the session_id. This helps distinguishing files across different users. After submitting the form to create the actual entity, you can retrieve the files from the orphanage by using only your session id. If the form was valid you can move the files from the temporary orphanage directory to the final destination of your files.

    This method has some pitfalls:

    • The orphanage directory itself should be cleaned on a regular basis using a cron job or the like.
    • If a user will upload files and choose not to submit the form, but instead start over with a new form, the newly uploaded files are going to be moved in the same directory. Therefore you will get both the files uploaded the first time and the second time after getting the uploaded files.

    This is not the ultimate solution to this problem but more of a workaround. It is in my opinion however cleaner than using temporary entities or session based storage systems.

    The mentioned bundle is available on Github and supports both Orphanage and the jQuery File Uploader plugin.

    1up-lab/OneupUploaderBundle

提交回复
热议问题