python - 从 html 表单中获取图像的 Python 问题
问题描述
想要从 HTML 表单到 python 脚本的图像我怎么能这样做,views.py 是我的 python 脚本。
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form action="views.py" method="post" enctype="multipart/form-data">
Image:
<input type="file" name="image" accept="image/*" id="image" required>
<input type="submit" name="submit" value="submit" required>
</form>
</body>
</html>
解决方案
您必须从 html 上传文件并保存在特定文件夹中。然后您可以再次从views.py 中使用该图像。
yourUploadImage.html:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form action="views.py" method="post" enctype="multipart/form-data">
Image:
<input type="file" name="image" accept="image/*" id="image" required>
<input type="submit" name="submit" value="submit" required>
</form>
</body>
</html>
路线.py:
@app.route("/imagesubmit", methods=['GET', 'POST'])
def imagesubmit():
if request.form.get('submit') == 'submit':
f = request.files['image']
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('viewimage'))
return render_template('yourUploadImage.html', form=form )
查看 :
路线.py
@app.route("/viewimage", methods=['GET', 'POST'])
def viewimage():
myimage = fnmatch.filter(os.listdir(os.path.join(app.static_folder, "img")))
return render_template('viewImage.html', item=item, myimage=myimage )
查看图片.html
{% for photo in myimage %}
<a target="_blank" href="{{ url_for('static' , filename='img/' + photo) }}"><img src="{{ url_for('static' , filename='img/' + photo) }}" class="img-rounded" alt="Cinque Terre" width="304" height="236" ></a>
{% endfor %}
在 config.py 中:
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
UPLOAD_FOLDER = os.getcwd() + '/app/static/img/'
你可能需要稍微改变一下。但是逻辑是一样的。将图像保存在文件夹中并从那里提取以供查看。
推荐阅读
- arrays - 如何使用对象数组索引堆叠元素?
- azure - 利用 Azure 资源管理 REST API
- r - 用户上传 csv 文件时渲染表
- wordpress - Wordpress 和 Woocommerce - 我可以将可重用块添加到默认为 HTML 编辑器而不是古腾堡编辑器的类别吗?
- arduino - Seeed Wio 终端对 GET 请求无响应
- mysql - 禁用严格模式后无法执行 SQL 文件事件
- windows - 记录文件创建者的最佳实践
- python - 获取 Microsoft Visual Studio 2019 以正确突出显示 Python 语法错误
- docker - Docker 桌面无法在 Windows 上启动
- javascript - 在 firebase 中验证电子邮件时遇到问题,applyActionCode 一直说我的代码无效