[英]Python Flask, Uploading a file - raise BuildError(endpoint, values, method)
I followed this tutorial for uploading files to a webserver via Flask, with the notable exception of the return part. 我遵循了本教程,通过Flask将文件上传到Web服务器,但值得注意的是返回部分除外。
My intention is to upload pictures. 我的意图是上传图片。
Here's my code: 这是我的代码:
from flask import Flask, request, jsonify, json, Blueprint, redirect, url_for, send_from_directory
from werkzeug import secure_filename
app = Flask(__name__)
allowed_extensions = set(['png', 'jpg', 'jpeg', 'gif', 'bmp'])
folder_upload = '/Users/myusername/Documents/Project_Upload/'
@CustomersAPI.route('/customers/addpicture', methods=['POST'])
def add_picture():
file = request.files['value']
print file.filename
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 str(url_for('uploaded_file', filename=filename))
return "Unable to upload."
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in allowed_extensions
if __name__ == '__main__':
app.config['UPLOAD FOLDER'] = folder_upload
app.run(host = '0.0.0.0', debug=True)
As I do not have the HTML files yet (hence the commented out return redirect
), I use CocoaRestClient for testing, and here are the parameters I used: 由于我还没有HTML文件(因此注释掉了
return redirect
),因此我使用CocoaRestClient进行测试,这是我使用的参数:
Everything okay so far, until I hit the Submit button. 到目前为止一切正常,直到我点击“提交”按钮。 Then, the following error appears:
然后,出现以下错误:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/myusername/Documents/Project_Omnimoda/API/main.py", line 192, in add_picture
return redirect(url_for('uploaded_file', filename=filename))
File "/Library/Python/2.7/site-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/Library/Python/2.7/site-packages/werkzeug/routing.py", line 1649, in build
raise BuildError(endpoint, values, method)
BuildError: ('uploaded_file', MultiDict([('filename', '3611571-dc_holiday.jpg')]), None)
Funny thing is, the image '3611571-dc_holiday.jpg' actually did get copied from my Downloads folder to the Project_Upload folder, so it worked, there's just an error that I'm not sure how to solve. 有趣的是,图像“ 3611571-dc_holiday.jpg”实际上确实从我的“下载”文件夹中复制到了Project_Upload文件夹中,因此它可以正常工作,只是有一个我不确定如何解决的错误。
Any ideas? 有任何想法吗? Thanks.
谢谢。
You haven't defined the uploaded_file
endpoint. 您尚未定义
uploaded_file
端点。 You need to do that. 您需要这样做。
@CustomersAPI.route('/customers/SOMETHINGOGESTHERE')
def uploaded_file(filename):
# Do something here with the file, like return it.
return send_from_directory(
folder_upload, filename, as_attachment=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.