我实际上已经按照诸如http://flask.pocoo.org/docs/0.10/patterns/fileuploads/之类的说明进行了很长时间的讨论 甚至尝试使用flask-uploads(我无法正确导入,因为它一直说我无法导入扩展名)。所以我只想坚持使用Flask上传照片的方法。
无论如何,我认为我的Flask-RESTFUL非常标准:
from flask import make_response, Flask, request, jsonify, redirect, url_for
from flask.ext.sqlalchemy import SQLAlchemy
from flask_restful import reqparse, abort, Api, Resource
from flask.ext.httpauth import HTTPBasicAuth
from werkzeug import secure_filename
import os
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://thanks:foryourhelp@localhost'
UPLOAD_FOLDER = '~/Documents/app/static'
ALLOWED_EXTENSIONS = set(['pdf', 'png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOADS_FOLDER'] = UPLOAD_FOLDER
api = Api(app)
auth = HTTPBasicAuth()
class uploadPhoto(Resource):
decorators = [auth.login_required]
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('image', type = str, default="")
self.reqparse.add_argument('lat', type = float, default="")
self.reqparse.add_argument('lon', type = float, default="")
super(uploadPhoto, self).__init__()
def post(self):
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))
api.add_resource(uploadPhoto, '/uploads/')
所以我想通过上传照片来测试它并使用python的Requests库上传文件,并且抛出错误的代码行是:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
KeyError: 'UPLOAD_FOLDER'
是因为我正在使用flaskRESTFUL吗?我有一种使用资源设置api的不同方法。无论如何,我被困住了,并希望得到这个工作,谢谢。
答案 0 :(得分:0)
您拥有app.config['UPLOADS_FOLDER'] = UPLOAD_FOLDER
,然后在post
功能中使用app.config['UPLOAD_FOLDER']
。您应该使用app.config['UPLOADS_FOLDER']
。