瓶文件上传和处理 [英] Bottle file upload and process
问题描述
我使用这里描述的确切代码:
http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads
我的问题是:
- 将上传的文件储存在内存中或磁盘上的特定位置Flask,两者兼而有之)?
- 将上传的文件直接提供给没有.read()的其他工具,然后手动将这些字节保存到磁盘上的指定文件? >
- 以文件作为参数启动系统命令的最佳方法是什么?是否有可能直接将路径传递给现有的文件? 好了,让我们打破这一点。
完整的代码是:
$ b
HTML:
< form action =/ uploadmethod =postenctype =multipart / form-data>
< input type =textname =name/>
< input type =filename =data/>
< / form>
PYTHON CODE:
请求
@route('/ upload',method ='POST')
def do_upload():
name = request.forms.name
data = request.files.data
如果名字和数据和data.file:
raw = data.file.read()#这对大文件是危险的
filename = data.filename
返回你好%s!你上传了%s(%d个字节)。 %(名称,文件名,len(原始))
返回您错过了一个字段。
(从您提供的文档中)
首先,我们可以看到,我们首先从名称
和数据
html表单,并将它们分配给变量 name
和 data
。这非常简单。然而,接下来我们将变量 raw
赋值给 data.file.read()
。这基本上是把文件上传到变量 raw
中。这就是说,整个文件在内存中,这就是为什么他们把这是危险的大文件作为旁边的评论线。
这就是说,如果你想把这个文件保存到磁盘上,可以这样做(但是要小心):
以open(filename,'w')作为open_file:
$ p $ > b
open_file.write(data.file.read())
至于其他问题:
<1>什么是启动系统命令的最好方法文件作为参数?是否可以直接将路径传递给现有文件?
您应该看到
子流程
模块,特别是Popen
: http ://docs.python.org/2/library/subprocess.html#popen-constructor
2。上传的文件是否可以直接使用其他工具没有.read(),然后手动保存th e字节到磁盘上指定的文件?
是的,你可以传递文件数据而不保存到磁盘,但是,被警告的内存消耗是东西看。但是,如果这些工具不在python中,你可能正在处理管道或子进程来传递数据到这些工具。
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?
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".
这篇关于瓶文件上传和处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!