On 10/27/06, Robin Haswell <[EMAIL PROTECTED]> wrote:
>
> I need to get the filename of an uploaded file from CherryPy. I can't
> write this to a different (known) file because I don't seem to be able
> to get a filename out of os.tmpfile(). I need the filename to pass to
> pysqlite2 - I'm uploading sqlite databases which need to be read. If
> anyone can let me know how to get the filename of an uploaded file (I
> assume it's some temp file), or any other solution to my problem, it
> would be very much appreciated.
>
> I don't really want my own method of generating temp filenames - I'd
> prefer to leave symlink attack prevention to the professionals.
>

The temporary files created during file uploads
(tempfile.TemporaryFile instances) do not have names on all platforms,
you will have to create a new file and copy the data into it.

You can get a named temporary file out of the tempfile module
(tempfile.NamedTemporaryFile).

Something like this probably would work::

def upload_form(self, formfile):
    import tempfile
    import shutil
    from pysqlite2 import dbapi2 as sqlite

    newfile = tempfile.NamedTemporaryFile()
    shutil.copyfileobj(formfile.file, newfile)
    newfile.flush()
    con = sqlite.connect(newfile.name)

-bob

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to