Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottle file upload and process

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:

  • Do bottle store uploaded file in memory or on a specific place on the disk (or perhaps like Flask, a bit of both)?
  • Will the uploaded file be directly available to other tools without .read() and then manually saving the bytes to a specified file on disk?
  • 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?
like image 248
agnsaft Avatar asked Jan 12 '13 18:01

agnsaft


People also ask

What is a template engine in bottle?

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.

How do I map get requests in bottle?

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.

How do I install bottle in Python?

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.

What is a file uploader and how to create one?

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">.


2 Answers

Ok, let's break this down.

The full code is:

HTML:

<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="text" name="name" />
  <input type="file" name="data" />
</form>

PYTHON CODE:

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".

like image 97
IT Ninja Avatar answered Oct 18 '22 01:10

IT Ninja


 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)
like image 31
Мостів Дмитро Avatar answered Oct 17 '22 23:10

Мостів Дмитро