无法在 Heroku 上使用 Flask 上传文件 [英] Unable to upload files with Flask on Heroku
问题描述
我正在尝试添加一个文件添加按钮,用于将文件上传到我的 Flask Heroku 应用程序的文件夹中,但是当我点击提交按钮时出现应用程序错误.
I'm attempting to add a file addition button that uploads a file to a folder on my Flask Heroku app but I get an application error when I hit the submit button.
class PhotoForm(Form):
photo = FileField('Your photo')
submit = SubmitField('Submit')
@app.route('/upload', methods=('GET', 'POST'))
def upload():
form = PhotoForm()
if form.validate_on_submit():
filename = secure_filename(form.photo.data.filename)
form.photo.data.save("uploads/" + filename)
else:
filename = None
return render_template('upload.html', form=form, filename=filename)
这里是upload.html:
Here is the upload.html:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block page_content %}
<form action="uploads/" method="POST" enctype="multipart/form-data">
{{ wtf.quick_form(form)}}
</form>
{{filename}}
{% endblock %}
这是它抛出的错误:
2015-01-21T20:52:24.834249+00:00 heroku[router]: sock=backend at=error code=H18
desc="Request Interrupted" method=POST path="/uploads/" host=xxxx.h
erokuapp.com request_id=18dabdc3-400e-4c8b-ba8a-8d52d4cc1cb5 fwd="8.34.183.2" dy
no=web.1 connect=3ms service=6ms status=503 bytes=568
推荐答案
Heroku 正在中断将数据存储在 uploads/
的请求,因为他们更喜欢你使用临时存储 tmp/
Heroku is interrupting the request to store data at uploads/
because they prefer you use temporary storage at tmp/
当 dyno 重新启动时,任何 tmp/
文件都将不存在,因为 Heroku 文件系统是 短暂的.
Any tmp/
files will not be there when the dyno restarts because Heroku filesystems are ephemeral.
遵循 12-factor 应用原则,Heroku 希望如果您的应用需要存储,您将使用存储服务 -- 流行的解决方案是 AWS S3 也可作为 插件.
Following the 12-factor app principles, Heroku expects that if your app needs storage you'll use a storage service -- the popular solution is AWS S3 also available as an add-on.
这篇关于无法在 Heroku 上使用 Flask 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!