이상훈
이상훈

Reputation: 11

flask file download method not allowed error

flask code

@app.route('/usecase/get/excel/<heads>', methods=['GET', 'POST'])
def get_excel(heads):
    headers = {"Content-Disposition": "attachment; filename=%s" % "usecase.xls"}
    with open("usecase.xls", 'r') as f:
        body = f.read()
    return Response(response=(body, headers))

html code

<form action="/template/usecase/get/excel/" method="post">     
   <button type="submit" class="btn btn-info">download</button>
</form>                                                        

if click download button, error occured "Method not allowed".. and how to download server's file? this is exactly program?

Upvotes: 1

Views: 147

Answers (1)

Mikael Hernrup
Mikael Hernrup

Reputation: 71

Are you matching the right route? /template/ does not seem to be part of your routing rules unless its some root prefix. Secondly the heads attribute is not optional so you will not hit that route anyway with that request.

On a more picky note: Why are you using a POST request to fetch the data when you clearly imply that you support the GET version.

Upvotes: 2

Related Questions