Thursday, February 21, 2013

Coldfusion Web Application Saving to DropBox

Every now and again, I'm working on a project that renders something that an administrative user needs to take with them; be it a pdf, or a csv representation of a report. Either way, once it is completed, I used to use coldfusion's cfcontent tag to dump the stream to the browser....which I've always thought of as pulling a fast-one on the browser, tricking it into thinking it is a physical file on disk to be downloaded, and Bob's your uncle.

This works, but let's be honest, cfcontent seems a bit of a sketchy way of delivering a file.... but it works... unless you're on shared hosting, in which case it's disabled... or will be, or should be, or will-be-until-you're-in-production.

I started out looking through the API for Dropbox, to try and figure out how to get a web application to create a new file in dropbox, which is totally possible... but much too complicated, as it has to be keyed to a specific dropbox account.

Then it dawned on me... I can create files on the disk... cfdocument allows you to specify a file name, so does cfimage, and cffile lets you create almost anything... Dropbox (and Google Drive) , are after all just a folder on the disk.. which is controlled by dark magic :-)

And this idea actually works... you just target the Dropbox folder, get your web app to store the generated file here, and the cloud-storage mechanisms do the heavy lifting for you. You could even create a dropbox folder hierarchy to keep things tidy.