When setting up a Flask server, we can try to receive the file user uploaded by
imagefile = flask.request.files['imagefile']
filename_ = str(datetime.datetime.now()).replace(' ', '_') + \
werkzeug.secure_filename(imagefile.filename)
filename = os.path.join(UPLOAD_FOLDER, filename_)
imagefile.save(filename)
logging.info('Saving to %s.', filename)
image = exifutil.open_oriented_im(filename)
When I am looking at the Klein
documentation, I've seen http://klein.readthedocs.io/en/latest/examples/staticfiles.html
, however this seems like providing file from the webservice instead of receiving a file that's been uploaded to the web service. If I want to let my Klein
server able to receive an abc.jpg
and save it in the file system, is there any documentation that can guide me towards that objective?
As
Liam Kelly
commented, the snippets from this post should work. Usingcgi.FieldStorage
makes it possible to easily send file metadata without explicitly sending it. A Klein/Twisted approach would look something like this:For whatever reason, my Python 3.4 (Ubuntu 14.04) version of
cgi.FieldStorage
doesn't return the correct results. I tested this on Python 2.7.11 and it works fine. With that being said, you could also collect the filename and other metadata on the frontend and send them in an ajax call to klein. This way you won't have to do too much processing on the backend (which is usually a good thing). Alternatively, you could figure out how to use the utilities provided by werkzeug. The functionswerkzeug.secure_filename
andrequest.files
(ie.FileStorage
) aren't particularly difficult to implement or recreate.