Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 548
T.J. Avatar asked Jan 21 '15 20:01

T.J.


People also ask

Does Heroku allow file upload?

We use direct uploads. Files are uploaded directly to the cloud from your user's browser, without passing through your application. Adding direct uploads to your app allows you to offload the storage of static files from your app. This is crucial on Heroku, because your app's dynos have an ephemeral filesystem.

How do I upload files to my Flask?

Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.

Can you use Flask with Heroku?

Deploying Flask App on Heroku STEP 1 : Create a virtual environment with pipenv and install Flask and Gunicorn . STEP 2 : Create a “Procfile” and write the following code. STEP 3 : Create “runtime. txt” and write the following code.


1 Answers

This is incorrect, Heroku does not actually interrupt requests at /uploads. You are hitting the issue described here.

The other answer is correct that any files not checked into git will be gone after a dyno cycle or rebuild, and that 12 factor principles should be followed, and if you need to keep the files for any length of time after upload, they should be sent to S3.

However, the H18 error code most often means that the socket successfully connected, some data was sent as part of a response by the app, but then the socket was destroyed without completing the response.

There aren't any tracebacks in your logs, so (from the linked help article above):

you'll need to look more closely at the handlers for the specific request that's failing. Logging each step of the response, including the x-request-id header, can help.

Here are the docs on request routing on Heroku: https://devcenter.heroku.com/articles/http-routing

like image 123
cfactoid Avatar answered Oct 12 '22 09:10

cfactoid