1

I am trying to use DropzoneJs.

I have a form with some text fields and a dropzone for file attachments. I did it so that after a file is successfully uploaded to the server (after being automatically processed by dropzone), I'll just add hidden input fields with the filenames as value along with all the other textfield data in the form (for database). However if I have dropped files in the dropzone and has been successfully been uploaded to the server, and for some reason the form won't be submitted (eg. page refreshed or tab closed), then there won't be a need to save the filenames into the database. How do I remove the files in the server just before a page refresh or tab closing?

Paul Ryan Lucero
  • 521
  • 1
  • 3
  • 16
  • 1
    Why not upload the files when the form is submitted instead? – M. Eriksson Jul 29 '17 at 14:25
  • I agree with Magnus, but if you want to keep the auto-upload, why not add a flag to the file in the database, clear it if the form is submitted, and periodically remove all flagged files? –  Jul 29 '17 at 14:26
  • @MagnusEriksson dropzone has a different url with the form – Paul Ryan Lucero Jul 29 '17 at 14:29
  • So change it to be the same: https://stackoverflow.com/questions/19893962/how-to-get-dropzone-js-to-upload-files-only-when-a-submit-button-is-clicked – M. Eriksson Jul 29 '17 at 14:31
  • @MagnusEriksson the first form contains text fields and dropzone has file uploads. if I use the same url, then the same function will be invoked to handle either requests. – Paul Ryan Lucero Jul 29 '17 at 14:35
  • Have one form, use dropzone to add files and post it all to one URL that handles the file uploads and processes the other fields. https://stackoverflow.com/questions/17872417/integrating-dropzone-js-into-existing-html-form-with-other-fields – M. Eriksson Jul 29 '17 at 14:41
  • @MagnusEriksson the filenames are saved in a different table `post_materials` and the other fields are saved in `posts` so i need to process the other fields first to get the id of the new saved post and attach it as foreign key to `post_materials` – Paul Ryan Lucero Jul 29 '17 at 14:43
  • That's totally irrelevant. If everything is posted/uploaded at the same time, you can store what ever you want in the database in what ever order you need. – M. Eriksson Jul 29 '17 at 14:45

1 Answers1

2

1) A good idea to do these kind of things ( at least how I do it ), is that you save all the images immediately ( with filenames ), and when the user closes the page and gets back to it in a while, they all show up again. So something like this: Let's say you have a table users and a table images. When he uploads images to the server, they will be saved in the server and in the table images even without submitting the form. When the user closes the page, and gets back to it, he will have all the uploaded images again.

2) If you don't want this kind of thing, in the images table you can add a column with the name "draft" or something like that, so when the user uploads pictures, all the images are drafts, and when he submits the form, then they all become un-drafted. So with this, when users close the window, images are still drafted and nobody won't see them. With this, you can then run a "cronjob" that deletes all the draft images from the server, or you can do that on the user's next login.

gencblakqori
  • 104
  • 7
  • the filenames are saved in a different table `post_materials` and the other fields are saved in `posts` so i need to process the other fields first to get the id of the new saved post and attach it as foreign key to `post_materials` – Paul Ryan Lucero Jul 29 '17 at 14:44
  • so i can't save the files first in the db – Paul Ryan Lucero Jul 29 '17 at 14:44
  • You can create that post before hand as a draft. So when user clicks on "create post", you can make a draft for that, so you then have a post id, and then when you submit the form and save everything in "posts", you can undraft it. – gencblakqori Jul 29 '17 at 14:48
  • I don't get why you want to over-complicate this? Just have one form that submits everything at the same time. No need for any cron jobs or "drafts" or anything. (there is such a thing as over-engineering things). – M. Eriksson Jul 29 '17 at 14:51
  • Depends on the project man, this is not over complicated ( it's basically the same logic as Wordpress does it with pages and posts). He either way has to store the images on the post_materials table, so basically only adding one column for making them as drafts is not that much of a problem. – gencblakqori Jul 29 '17 at 14:55
  • ...plus adding a cron job that's basically a garbage collector + extra db calls since you would need to update/fetch the "pre saved" data/id's etc. In what type of project could you _not_ post everything in one go? The user will have the exact same experience (except from actually not uploading stuff until the user actually clicks on submit, which in my opinion is how it _should_ be). And just because WP does something, doesn't make it a good idea. – M. Eriksson Jul 29 '17 at 15:03
  • I didn't say that just because WP uses it, it's a good idea. It's just an idea, you can use it, you can not. Depends on you! – gencblakqori Jul 29 '17 at 15:07
  • 2
    Btw here is just one example: Let's say you have dropzone in your form. If you want to upload 20 pictures at one time, each of them 2mb, it is much faster if you have it like this than just upload them at one time. In this way, each of these have their own thread and can be proceeded independently, so muuuuuuch quicker than having to upload 40mb at one time! – gencblakqori Jul 29 '17 at 15:19