瓶/ Apache的提交按钮上传文件 [英] Flask/Apache submit button for file upload
问题描述
我在后面跟着跑阿帕奇烧瓶中的应用,我的index.html页面上我有一个文件的上传按钮和一个提交按钮,在这里看到:
<表ID =package_form行动=方法=POST>
< DIV>
< P>上传软件包:其中; / P>
< P><输入ID =upload_button类型=文件级=BTN BTN-默认BTN-XS>< / P>
< P><输入ID =submit_button类型=提交级=BTN BTN-成功值=上传>
< / DIV>
< /表及GT;
我希望将帖子发送请求和烧瓶会赶上它,做文件上传如本文件:
从瓶进口render_template,请求,响应url_for
从应用程序导入
从WERKZEUG进口secure_filename##载规格##
UPLOAD_FOLDER ='/ tmp目录/'
ALLOWED_EXTENSIONS =集(['DEB'])高清allowed_file(文件名):
回报'。在文件名和\\
filename.rsplit('。',1)[1]在ALLOWED_EXTENSIONS##索引页的东西##
@ app.route('/索引,方法= ['GET','POST'])
高清指数():
##名的Kerberos
secuser = request.environ.get('REMOTE_USER') 用户= {'缺口':secuser}
##文件上传的东西
如果request.method =='POST':
文件= request.files ['文件']
如果文件和allowed_file(file.filename):
文件名= secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER,文件名))
返回重定向(url_for('/索引,
文件名=文件名))##主回
返回render_template(index.html的
用户=用户)
上传文件按钮效果很好,正确做的一切,它只是,当提交按钮被pressed我得到一个400错误,所以它必须是对事物的烧瓶一面的东西,但我不完全相信它会是什么。
任何帮助将是非常美联社preciated:)
如果request.method =='POST':
文件= request.files ['文件']
如果文件和allowed_file(file.filename):
文件名= secure_filename(file.filename)
file.save(os.path.join(/ tmp目录/,文件名))
这做的伎俩!
虽然你还需要将它添加到index.html的(名字=文件到upload_button)
<表ID =package_form行动=方法=POST>
< DIV>
< P>上传软件包:其中; / P>
< P><输入ID =upload_button类型=文件级=BTN BTN-默认BTN-XSNAME =文件>< / P>
< P><输入ID =submit_button类型=提交级=BTN BTN-成功值=上传>
< / DIV>
< /表及GT;
I have a flask application running behind apache, and on my index.html page I have a file upload button and a submit button, as seen here:
<form id="package_form" action="" method="POST">
<div>
<p>Upload Packages:</p>
<p><input id="upload_button" type="file" class="btn btn-default btn-xs"></p>
<p><input id="submit_button" type="submit" class="btn btn-success" value="Upload">
</div>
</form>
which I was hoping would send the post request and flask would catch it and do the file uploading as shown in this file:
from flask import render_template, request, Response, url_for
from app import app
from werkzeug import secure_filename
## uploading specs ##
UPLOAD_FOLDER = '/tmp/'
ALLOWED_EXTENSIONS = set(['deb'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
## index page stuff ##
@app.route('/index', methods = ['GET', 'POST'])
def index():
## kerberos username
secuser = request.environ.get('REMOTE_USER')
user = { 'nick': secuser }
## file uploading stuff
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
return redirect(url_for('/index',
filename=filename))
## main return
return render_template("index.html",
user = user)
The upload file button works well, and does everything correctly, it's just that when the Submit button is pressed I get a 400 error, so it has to be something on the flask side of things, but I'm not exactly sure what it could be.
Any help would be much appreciated :)
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join("/tmp/", filename))
This does the trick!
Although you also need to add this to index.html (name="file" to the upload_button)
<form id="package_form" action="" method="POST">
<div>
<p>Upload Packages:</p>
<p><input id="upload_button" type="file" class="btn btn-default btn-xs" name="file"></p>
<p><input id="submit_button" type="submit" class="btn btn-success" value="Upload">
</div>
</form>
这篇关于瓶/ Apache的提交按钮上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!