Python 在 Flask 中上传图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44926465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload image in Flask
提问by Teor9300
I have to upload some images in static folder of my project directory, but i don't know how to say it to my code. In the follow code.py i'm able to upload an image and store it in the project directory at the same level of static folder, but i would that this image can be store INSIDE static folder.
我必须在我的项目目录的静态文件夹中上传一些图像,但我不知道如何对我的代码说出来。在下面的 code.py 中,我可以上传图像并将其存储在与静态文件夹相同级别的项目目录中,但我希望该图像可以存储在静态文件夹中。
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return render_template('end.html')
What i have to do?? Thanks guys
我该怎么办??谢谢你们
回答by PYA
you need to define the upload folder
你需要定义 upload folder
from the flask
documentation
从flask
文档
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
So your code would be UPLOAD_FOLDER = '/path/to/static/images'
or something like that
所以你的代码将是UPLOAD_FOLDER = '/path/to/static/images'
或类似的东西
回答by Dulangi_Kanchana
Try to first store the image jpeg or jpg in a static folder
inside the static folder the image is stored
尝试先将图像 jpeg 或 jpg 存储在存储图像的静态文件夹
内的静态文件夹中
then in the html file
然后在html文件中
<img src="{{url_for('static', filename='Hermes.png')}}" align="middle" />
add this code line
添加此代码行
In app.py the code
在 app.py 中的代码
from flask import Flask, render_template, redirect, url_for, request
# Route for handling the login page logic
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/registerV')
def registerV():
return render_template('registerV.html')
In register.html the code
在 register.html 中的代码
<html>
<head>
</head>
<body>
<div class="bd-example" align="middle">
<img src="{{url_for('static', filename='Hermes.png')}}" align="middle" />
</div>
</body>
</html>
回答by Paul
For some reason, @PYA 's code won't work for me, but I made a small change:
I used 'path/to/the/uploads'
instead of '/path/to/the/uploads'
and everything works fine.
出于某种原因,@PYA 的代码对我不起作用,但我做了一个小改动:
我使用了'path/to/the/uploads'
而不是,'/path/to/the/uploads'
一切正常。