0

I'm following the Flask tutorial for image uploading. I also decided to tinker with it a bit so that I can add other features. The website loads locally, but when I actually click on the upload button (after selecting a .png image), I get the following error:

Bad Request. The browser (or proxy) sent a request that this server could not understand.

I've read something about adding an else statement here: Form sending error, Flask But I'm not sure how to accommodate it for my needs. Here is my code:

from flask import Flask, request, session, g, redirect, url_for, \
  abort, render_template, flash

from werkzeug import secure_filename

import os

DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

#Add parameters for image uploads:
UPLOAD_FOLDER = '/upload_folder/'
ALLOWED_EXTENSIONS = set(['png','jpg','jpeg'])


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


#@app.route('/')
#@app.route('/<name>')


#Image uploads:
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=multpart/form-data>
    <p><input type=file name=file>
       <input type=submit value=Upload>
  </form>
  '''

#Handle errors:
@app.errorhandler(404)
def page_not_found(error):
  return render_template('page_not_found.html'),404

#render template:
def flaskr(name=None):
  return render_template('hello.html',name=name)


#Handle errors:
@app.errorhandler(404)
def page_not_found(error):
  return render_template('page_not_found.html'),404



if __name__ == '__main__':
  app.run()
Community
  • 1
  • 1
Arturo
  • 3,679
  • 6
  • 34
  • 43

1 Answers1

3

There is a typo in your html code:

<form action="" method=post enctype=multpart/form-data>

should be multipart and not multpart.

doru
  • 9,022
  • 2
  • 33
  • 43