Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

python - Unable to upload files with Flask on 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)

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 %}

Here is the error it throws:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Heroku is interrupting the request to store data at uploads/ because they prefer you use temporary storage at tmp/

Any tmp/ files will not be there when the dyno restarts because Heroku filesystems are ephemeral.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...