I'm trying to create a simple web application where a user can upload a file for my program to work with on the backend.
The application is going to run on a UNIX system.
How do I save their uploaded file locally (in the same directory) for my program to then work with?
class Root():
@cherrypy.expose
def index(self):
return """
<html><body>
<center>
<h2>Upload file</h2>
<form action="upload" method="post" enctype="multipart/form-data">
filename: <input type="file" name="myFile" /><br />
<input type="submit" />
</form>
</center>
</body></html>
"""
This example is from: https://docs.cherrypy.org/en/latest/_modules/cherrypy/tutorial/tut09_files.html
If you want to save the file to your working directory, you will need to first read()
the file and then write()
it.
uploaded_file = myFile.file.read()
with open('saved_file_name.txt', 'w') as f:
f.write(uploaded_file)