i need help uploading and storing files with web.py
this is my code so far
import web
urls = (
'/hello', 'index',
'/hello/upload', 'upload'
)
app = web.application(urls, globals()) # handels http request that aks for
urls
# the base ells lpthw.web to use the templates/layout.html file as the base
template for all the other templates
render = web.template.render('templates/', base="layout")
# must use port 127.0.0.1:5000
class index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s" % (form.greet, form.name)
return render.index(greeting = greeting)
class upload(object):
def POST(self):
x = web.input(files={})
filedir = '/project/webtest/templates' # directory were you want
to save the file
if 'files' in x: # check if the file -object is created
filepath=x.files.filename.replace('\\','/') # replace the
windows -style slashes with linux ones
filename=filepath.split('/')[-1] #splits the and chooses the
last part (the filename with extension
fout = open(filedir +'/'+ filename,'w') # creates the file
where the uploaded file should be stored
fout.write(x.files.file.read()) #writes the uploaded file to
the newly created file.
fout.close() #closes the file
else:
return "Error no file"
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
i keep geting a
<type 'exceptions.IOError'>
help :O
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/webpy/-/ftPQBm0FqKMJ.
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/webpy?hl=en.