Charles L.
Charles L.

Reputation: 6295

How can I create "upload" files with web2py

I have an app running on web2py. For this app I want to store a lot of files to a database for each user, which they can either upload from their own computer or they can create online and save. These files can be either text or binary files, but if they are created in my app, they will be text. So I have 2 ways files come in that I have to handle:

1) Uploads through a form. The database has a "file" field of type "upload" which I store using:

db.allfiles.insert(filename=filename, \ 
    file=db.allfiles.file.store(file.file,filename),user=me) 

This creates a file with a unique string attached to its name in the uploads directory. This solution is pretty easy.


2) I also need to store files that come in as strings via JSON call. I am not sure how to create "upload" type files and give them unique names in the uploads directory. Can anyone give any insight?

Thanks

Upvotes: 1

Views: 2452

Answers (1)

Nico de Groot
Nico de Groot

Reputation: 86

I think you can just the incoming data, turn it into an in-memory stream and store it like you do at 1)

import StringIO
fileHandle = StringIO.StringIO ( JSONvar )

Upvotes: 1

Related Questions