我正在使用 Flask 在我的服务器 8Ubuntu 18.04 上托管一个 Web 应用程序。
我的应用程序的目标是上传图片(或任何类型的文件)。但是,当尝试将文件上传到 img 文件夹时,出现权限错误:
ERROR:cloud:Exception on / [POST], referer: http://192.168.1.35/
Traceback (most recent call last):, referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app, referer: http://192.168.1.35/
response = self.full_dispatch_request(), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request, referer: http://192.168.1.35/
rv = self.handle_user_exception(e), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception, referer: http://192.168.1.35/
reraise(exc_type, exc_value, tb), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise, referer: http://192.168.1.35/
raise value, referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request, referer: http://192.168.1.35/
rv = self.dispatch_request(), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request, referer: http://192.168.1.35/
return self.view_functions[rule.endpoint](**req.view_args), referer: http://192.168.1.35/
File "/var/www/cloud/cloud/__init__.py", line 15, in upload_image, referer: http://192.168.1.35/
image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename)), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/werkzeug/datastructures.py", line 3066, in save, referer: http://192.168.1.35/
dst = open(dst, "wb"), referer: http://192.168.1.35/
PermissionError: [Errno 13] Permission denied: '/var/www/cloud/cloud/static/img/uploads/3E5A33B5-7E19-461A-B0C5-6835080675E7.jpeg', referer: http://192.168.1.35/
, referer: http://192.168.1.35/
AH00491: caught SIGTERM, shutting down
AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
AH00094: Command line: '/usr/sbin/apache2'
所以我知道存在权限错误。怎么可能解决这个问题?
我的文件结构如下:
我的代码(python3)__init__.py
如下:
import os
from flask import Flask, flash, request, redirect, url_for, render_template
app = Flask(__name__)
app.config["IMAGE_UPLOADS"] = "/var/www/cloud/cloud/static/img/uploads"
@app.route("/", methods = ["GET", "POST"])
def upload_image():
if request.method == "POST":
if request.files:
image = request.files["image"]
print(image)
image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename))
return "IMAGE SAVED"
return render_template('upload-image.html')
if __name__ == "__main__":
app.run()
对于upload-image.html,我如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HELLO</title>
</head>
<body>
<div>
<h1>This is a test</h1>
<form action="/" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name = "image" id="image">
<label for="image">Select Image</label>
</div>
<button type="submit">Upload</button>
</form>
</div>
</body>
</html>
当它在本地工作时,我没有收到此错误,但只有当我部署 t 以便它可以在与我的同一 LAN 中的计算机上访问时。
这是我正在关注的教程:https ://www.youtube.com/watch?v=6WruncSoCdI