fix the file upload resource

This commit is contained in:
Damien Churchill 2009-04-02 18:50:45 +00:00
parent 793e120957
commit 061c06a164

View File

@ -95,29 +95,30 @@ class Upload(resource.Resource):
Twisted Web resource to handle file uploads Twisted Web resource to handle file uploads
""" """
def http_POST(self, request): def render(self, request):
""" """
Saves all uploaded files to the disk and returns a list of filenames, Saves all uploaded files to the disk and returns a list of filenames,
each on a new line. each on a new line.
""" """
# Block all other HTTP methods.
if request.method != "POST":
request.setResponseCode(http.NOT_ALLOWED)
return ""
tempdir = os.path.join(tempfile.gettempdir(), "delugeweb") tempdir = os.path.join(tempfile.gettempdir(), "delugeweb")
if not os.path.isdir(tempdir): if not os.path.isdir(tempdir):
os.mkdir(tempdir) os.mkdir(tempdir)
filenames = [] filenames = []
for files in request.files.values(): for upload in request.args.get("file"):
for upload in files: f = tempfile.NamedTemporaryFile(dir=tempdir, delete=False)
fn = os.path.join(tempdir, upload[0]) f.write(upload)
f = open(fn, upload[2].mode) filenames.append(f.name)
shutil.copyfileobj(upload[2], f) f.close()
filenames.append(fn) request.setHeader("content-type", "text/plain")
return http.Response(http.OK, stream="\n".join(filenames)) request.setResponseCode(http.OK)
return "\n".join(filenames)
def render(self, request):
"""
Block all other HTTP methods.
"""
return http.Response(http.NOT_ALLOWED)
class Render(resource.Resource): class Render(resource.Resource):