Author Topic: Temporary downloadable files -- best practice?  (Read 2475 times)

0 Members and 1 Guest are viewing this topic.

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Temporary downloadable files -- best practice?
« on: September 16, 2011, 07:43:41 pm »
I'm taking user input (in a text box or whatever), and trying to make a file the user can then download.  What is the best way of managing the newly created files?

Should I assign them a unique name, based on a hash or something?  Or is there a way to preserve filenames without risking accidental name collision?

I really don't want a bunch of files to be created and slowly take up a huge amount of memory -- what's the best way of managing and purging temporary downloadable files?

(I'm trying to make something based of off Javascript and Python).
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Temporary downloadable files -- best practice?
« Reply #1 on: September 16, 2011, 07:51:40 pm »
Maybe this? http://docs.python.org/library/tempfile.html

Edit: On the note of preserving filename, perhaps you could create a temporary directory with tempfile.mkdtemp() and put your file in that.
« Last Edit: September 16, 2011, 07:56:54 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Temporary downloadable files -- best practice?
« Reply #2 on: September 16, 2011, 10:08:09 pm »
You might want to make a script that output the file you're generating without putting it in a temp file on disk (with print or something instead of the function you use to write to a file, I don't really know Python). You can then specify the MIME type and the filename with appropriate HTTP headers.
« Last Edit: September 16, 2011, 10:09:19 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.