我正在使用Apache 2和uWSGI在Ubuntu 18.04系统上运行flask应用程序。当我尝试将文件上传到目录时,它失败并显示500错误代码。该脚本写在__init__.py
中,位于:/var/www/FlaskApp/FlaskApp/__init__.py
。上传文件的目录位于:/var/www/FlaskApp/FlaskApp/uploads
。该脚本如下所示:
from flask import Flask, render_template, request, url_for, redirect,
send_from_directory, send_file, flash
from werkzeug.utils import secure_filename
import os
UPLOAD_FOLDER = '/var/www/FlaskApp/FlaskApp/uploads'
#ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/test/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
#if 'file' not in request.files:
#flash('No file part')
#return redirect(request.url)
file = request.files['file']
#if file.filename == '':
#flash('No selected file')
#return redirect(request.url)
#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 method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
"""
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
"""
if __name__ == '__main__':
app.run()
它显示HTML页面,但是当我选择文件后单击“提交”时,它失败。我试图同时使用和不使用注释来运行代码,但是仍然失败。我认为错误发生在:file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
我也尝试了几种不同的方法。应用程序的配置可能出问题了吗?我认为我没有真正的settings.py,如果应该的话。
非常感谢您的答案
Flask App错误日志显示以下内容:
ERROR:flask.app:Exception on /test/ [POST], referer: http:the.servers.ip.adress/test/
Traceback (most recent call last):, referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app, referer: http:the.servers.ip.adress/test/
response = self.full_dispatch_request(), referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request, referer: http:the.servers.ip.adress/test/
rv = self.handle_user_exception(e), referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception, referer: http:the.servers.ip.adress/test/
reraise(exc_type, exc_value, tb), referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise, referer: http:the.servers.ip.adress/test/
raise value, referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request, referer: http:the.servers.ip.adress/test/
rv = self.dispatch_request(), referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request, referer: http:the.servers.ip.adress/test/
return self.view_functions[rule.endpoint](**req.view_args), referer: http:the.servers.ip.adress/test/
File "/var/www/FlaskApp/FlaskApp/__init__.py", line 27, in upload_file, referer: http:the.servers.ip.adress/test/
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)), referer: http:the.servers.ip.adress/test/
File "/usr/local/lib/python3.6/dist-packages/werkzeug/datastructures.py", line 2725, in save, referer: http:the.servers.ip.adress/test/
dst = open(dst, 'wb'), referer: http:the.servers.ip.adress/test/
PermissionError: [Errno 13] Permission denied: '/var/www/FlaskApp/FlaskApp/uploads/an_image.jpg', referer: http:the.servers.ip.adress/test/
referer: http:the.servers.ip.adress/test/
ERROR:flask.app:Exception on /prov/ [POST], referer: http:the.servers.ip.adress/test/
Traceback (most recent call last):, referer: http:the.servers.ip.adress/test/
答案 0 :(得分:0)
好的,我找到了解决方案。在Flask应用程序内创建文件夹时,您必须为其赋予比默认权限更多的权限。在使用WinSCP的情况下,我只单击了每个框。
答案 1 :(得分:0)
我只是自己测试代码,以确保您具有对上载文件夹的权限,只需对其进行写权限即可,并且路径应该正确无误。