上传图片烧瓶错误

时间:2015-05-07 09:04:51

标签: python post flask

我按照Flask教程进行图片上传。我还决定修改一下,以便我可以添加其他功能。该网站在本地加载,但当我实际点击上传按钮(选择.png图像后)时,我收到以下错误:

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

我已经在这里阅读了一些关于添加else语句的内容: Form sending error, Flask 但我不确定如何满足我的需求。 这是我的代码:

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()

1 个答案:

答案 0 :(得分:3)

你的HTML代码中有一个拼写错误:

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

应该是multipart而不是multpart