Here's the code of my working "upload" url.  I'm saving the file, but the
value in x["fupFile"].file.read() could also be stored somewhere else, in
the session, or in a variable if you intend to use it later in this request.

Also note my use of os.path.join.  It's bad practice to hardcode slashes in
a path - it's not cross platform!  os.path.join will concatenate many items
together using the OS specific path.

class upload:

    def GET(self):

        return """This endpoint only accepts POSTS from file_upload.html"""

    def POST(self):

        x = web.input(fupFile={}, ref_id="")

        if x:

            # print x # ref_id

            # web.debug(x['fupFile'].filename) # This is the filename

            # web.debug(x['fupFile'].value) # This is the file contents

            # web.debug(x['fupFile'].file.read()) # Or use a file(-like)
object



            ref_id = (x.ref_id if x.ref_id else "")

            filename = "%s.tmp" % (ref_id)

            fullpath = os.path.join("tmp", filename)

            with open(fullpath, 'w') as f_out:

                if not f_out:

                    raise Exception("Unable to open %s for writing." %
(fullpath))

                f_out.write(x["fupFile"].file.read())  # writes the
uploaded file to the newly created file.



            # all done, we loop back to the file_upload.html page, but this
time include

            raise web.seeother(
"static/pages/file_upload.html?ref_id=%s&filename=%s" % (ref_id, filename))


On Wed, Jan 15, 2014 at 3:30 PM, Jessica Le <[email protected]> wrote:

> I actually kind of figured it out. I had to list the entire directory for
> it to work. So instead of
>
> filedir = '/project/webtest/templates'  # directory were you want to save
> the file
>
> I changed it to:
>
> filedir = 'C:/project/webtest/templates'  # directory were you want to
> save the file
>
> However, I'm trying to work on saving this in memory, since saving
> uploaded files to the local drive is BAD BAD practice. Any
> suggestions/links that I could refer to? I have searched using the REST web
> service, but there just isn't much documentation on it.
>
> Thanks!
>
>
> On Monday, January 13, 2014 12:44:45 PM UTC-6, Hugo Lol wrote:
>>
>> In which line are you getting the error? Running under Windows or Linux?
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/webpy.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to