I am using Bottle for uploading rather large files. The idea is that when the file is uploaded, the web app run (and forget) a system command with the uploaded file-path as an argument. Except for starting the system command with the correct file-path as an argument I do not need to save the file, but I need to be certain that the file will be available until the process completes the processing.
I use the exact code described here: http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads
My questions are:
A template engine is a library designed to combine templates with a data model to produce result documents. Bottle uses a simple template engine by default. We create directories and files for the application.
The HTTP GET method requests a representation of the specified resource. In Bottle, we can map GET requests with @route or @get decorators. The data is retrieved from request.query. The GET request is usually the default request method.
We use the pip3 tool to install Bottle. In the following example, we create a simple Bottle application. We create a project directory a Python file. The example sends a message to the client. We import the route decorator and the run function.
For users, a file uploader looks like a button on a website that opens a dialogue box where they can choose, attach, and submit files. Like this: At first, it may seem like there’s nothing complicated about developing a basic HTML file upload form using <input type="file">.
Ok, let's break this down.
The full code is:
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="file" name="data" />
</form>
from bottle import route, request
@route('/upload', method='POST')
def do_upload():
name = request.forms.name
data = request.files.data
if name and data and data.file:
raw = data.file.read() # This is dangerous for big files
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
return "You missed a field."
(From the doc's you provided)
So, first of all, we can see that we first pull the information from the name
and the data
in the html form, and assign them to the variables name
and data
. Thats pretty straight forward. However, next we assign the variable raw
to data.file.read()
. This is basically taking all of the file uploaded into the variable raw
. This being said, the entire file is in memory, which is why they put "This is dangerous for big files" as a comment next to the line.
This being said, if you wanted to save the file out to disk, you could do so (but be careful) using something like:
with open(filename,'w') as open_file:
open_file.write(data.file.read())
As for your other questions:
1."What would be the best way to start the system command with the file as an argument? Is it possible to just pass the path to an existing file directly?"
You should see the subprocess
module, specifically Popen
: http://docs.python.org/2/library/subprocess.html#popen-constructor
2."Will the uploaded file be directly available to other tools without .read() and then manually saving the bytes to a specified file on disk?"
Yes, you can pass the file data around without saving it to disk, however, be warned that memory consumption is something to watch. However, if these "tools" are not in python, you may be dealing with pipes or subprocesses to pass the data to these "tools".
with open(filename,'w') as open_file:
open_file.write(data.file.read())
dont work
you can use
data = request.files.data
data.save(Path,overwrite=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With