Flask resets the connection instead of returning a 413, when the file is to big
When MAX_CONTENT_LENGTHis definded, Flask should return an 413, but in my case it resets the connection with this error: Error 101 (net::ERR_CONNECTION_RESET): The connection was reset.
I attached a example built upon the file upload pattern from Flask.
I use Chrome 15.0.874.5.
import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
UPLOAD_FOLDER = 'uploads/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','bib','mp3'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
@app.route('/uploads/<filename>')
de开发者_运维知识库f uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)
@app.errorhandler(413)
def file_to_big(e):
    return 'File to big'
if __name__ == '__main__':
    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024
    app.run(debug=True)                    
I just had a similar issue where uploading a file to a flask dev server gave me the net::ERR_CONNECTION_RESET in chrome.
Basically, in the receiving view on the server, I check if the user's session has expired, and if it has, I return a redirect. For some reason this causes a connection reset. I'm guessing this is because the browser is still trying to send the file but the server doesn't want it (though I can't back that up with evidence).
Thought I'd put this here for anyone who lands here like I did.
It seems that it is only a problem using the Flask Dev sever. When I went live on a lighttp the redirect worked.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论