Skip to content

Instantly share code, notes, and snippets.

@saga
Created September 15, 2010 09:50
Show Gist options
  • Save saga/580494 to your computer and use it in GitHub Desktop.
Save saga/580494 to your computer and use it in GitHub Desktop.
# coding: utf-8
import web
urls = (
'/hello/(.*)', 'Hello',
'/upload', 'Upload',
'/list', 'List',
'/getimage/(.*)', 'GetImage',
)
filedir = './upload'
class Hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
class List:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
import os
fileslist = os.listdir(filedir)
headerStr = """
<html><head></head><body>
<p><a href="upload">Upload File</a></p> <br />
"""
str = ""
for filename in fileslist:
if len(filename) < 3 or filename[-3:] != "jpg":
continue
imgsrc = "<img src='http://apcndaeforum:8080/getimage/" + filename + "' height='320px' />"
str += imgsrc
endStr = "</body></html>"
print(str)
return headerStr + str + endStr
class GetImage:
def GET(self, filename):
web.header("Content-Type","image/jpeg")
fout = open(filedir +'/'+ filename, 'rb')
return fout
class Upload:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<br/><br/><input type="file" name="myfile" /> <br/>
<input type="submit" /> </form>
</body></html>"""
def POST(self):
inputfile = web.input(myfile={})
if 'myfile' in inputfile:
filepath= inputfile.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
filename=filepath.split('/')[-1]
fout = open(filedir +'/'+ filename, 'wb')
fout.write(inputfile.myfile.file.read())
fout.close()
raise web.seeother('/list')
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment