Sander Marechal
Sander Marechal

Reputation: 23216

How to associate files to records that have not been saved yet

I am working on a web application that allows you to write notes. I want to add some functionality to allow a user to add attachments to notes, but I have a little trouble figuring out the logic behind it.

I want it to work a bit like webmail or phpBB forum posts. You start a new message. There's a file upload input element there with an "add" button next to it. When you add a file, it is uploaded and you can continue to write your message. When you finally click "submit" it creates the note and associates the uploaded files with it. Here's some ASCII art:

Subject:     ______________

Message:     ______________
             ______________
             ______________

Attachments: some_file.txt
             resume.odt
             ______________ [Browse][Add]

             [Save]

But how can I associate the uploads with the note when it is still being written? It hasn't been saved yet. It has no ID. Normally I would add a database table that associates uploaded files with note IDs, but that doesn't work for a note that hasn't been saved yet. What I worry about is, when a user starts to write a new note, adds a file to it and then changes his mind, never saving the note (e.g. closing the browser). I don't want those uploaded files lingering around.

Thanks in advance!

Upvotes: 2

Views: 227

Answers (4)

Richard
Richard

Reputation: 5101

A little (!) late to the conversation, but another option might be to upload to a tmp dir - then when the message/post/form is submitted and saved you could move the relevant files out of the tmp dir into a permanent dir.

Then have a cron job running daily (eg) deleting files from the tmp folder that are older than a day.

Upvotes: 0

Roger Halliburton
Roger Halliburton

Reputation: 2021

Way back in the days before AJAX, if you wanted to attach a file to a form you would simply upload the file at time of form submission. Now we have all sorts of clever ways to transfer files, or at least two or three popular ways, some using Flash or jQuery. But you have spotted the major maintenance problem with these techniques: whenever a user uploads an image in parallel to filling out a form, there is no guarantee that the form will ever be submitted. You have to come up with some kind of timeout mechanism for those uploaded files, and that generally means starting up some kind of housekeeping process. You get to add another moving part to your architecture, and you'll need to monitor it periodically to make sure things don't get out of control.

Depending on the expected frequency and size of traffic, you need to define some parameters for when an orphaned file can be deleted. If you expect less than 100 uploads a day, then daily purges should be fine. In each purge, you want to delete any file that has been orphaned for a certain amount of time. To do that you'll need a mechanism for identifying the old files, and while you could compare the files to your table records this requires extra database resources that might impact the speed of your site. That is a hint that you should be moving those files somewhere else once they have been processed, making it easier to identify the potential orphans.

Now as you monitor this process you can decide if it needs to run more or less often. If you made a poor design decision it will be more painful to do that housekeeping, especially if it runs hourly or more often.

Good luck!

Upvotes: 2

Bjoern
Bjoern

Reputation: 16304

There are many ways of doing it. Here's the one I prefer at first glance: Generate the "mail ID" when the user clicks on "new mail", then you already have it when he wants to upload something.

A way more experimental idea: Generate a hash of the file (p.e. MD5 with a timestamp or something similar) when he uploads it. When he finally submits the mail, add the hash to the mail and use this as a key to the previous upload.

Upvotes: 1

Ilya Saunkin
Ilya Saunkin

Reputation: 19820

Alright, so he opens that URL where he sees the layout you managed to draw (kudos for ASCII art). Before rendering the page, you have created an empty note record which will provide you with ID. He uploads files (that go somewhere on the hard drive and you save just the paths in another database table, remember that file-note relationship is many-to-one), voila!, you already have an ID to associate with them! As of orphan files with abandoned note.. Well, there's a few options to handle that, but all of them end up to having another chapter in your web app, called 'maintenance'. I would keep last note ID in the user profile and wouldn't let him create a new note without saving or discarding previous one.

Upvotes: 2

Related Questions