【发布时间】:2021-03-28 14:07:14
【问题描述】:
我想使用 POST 请求将图像上传到 Flask 服务器。
这是我的 FLASK 服务器:
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/post_pic',methods=['POST','GET'])
def post_picture():
if request.method == 'POST':
file1 = request.files['image']
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
return_status = {"success":"true"}
return jsonify(return_status)
return render_template("show_pic3.html", file1=file1)
我的show_pic3.html:
<html>
<head>
<title>show_pic</title>
</head>
<body>
<img src="{{ file1 }} ">
</body>
</html>
我的要求:
pic={'image': open(r'C:\Users\winwo\OneDrive\Documents\testpic.jpg', mode='rb')}
r = requests.post('https://testing001.herokuapp.com/post_pic', files=pic)
print(r.text)
我不断收到的错误:
2021-03-28T12:39:36.301383+00:00 app[web.1]: [2021-03-28 12:39:36,299] ERROR in app: Exception on /post_pic [POST]
2021-03-28T12:39:36.301403+00:00 app[web.1]: Traceback (most recent call last):
2021-03-28T12:39:36.301404+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
2021-03-28T12:39:36.301405+00:00 app[web.1]: response = self.full_dispatch_request()
2021-03-28T12:39:36.301405+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
2021-03-28T12:39:36.301406+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-03-28T12:39:36.301406+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
2021-03-28T12:39:36.301406+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2021-03-28T12:39:36.301407+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2021-03-28T12:39:36.301408+00:00 app[web.1]: raise value
2021-03-28T12:39:36.301408+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
2021-03-28T12:39:36.301409+00:00 app[web.1]: rv = self.dispatch_request()
2021-03-28T12:39:36.301409+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2021-03-28T12:39:36.301410+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2021-03-28T12:39:36.301410+00:00 app[web.1]: File "/app/app_with_handler.py", line 147, in post_picture
2021-03-28T12:39:36.301411+00:00 app[web.1]: file1.save(path)
2021-03-28T12:39:36.301412+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/datastructures.py", line 3066, in save
2021-03-28T12:39:36.301412+00:00 app[web.1]: dst = open(dst, "wb")
2021-03-28T12:39:36.301418+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: 'app/static/uploads/testpic.jpg'
2021-03-28T12:39:36.302536+00:00 app[web.1]: 10.5.225.117 - - [28/Mar/2021:12:39:36 +0000] "POST /post_pic HTTP/1.1" 500 290 "-" "python-requests/2.25.1"
我的独角兽:
gunicorn app_with_handler:app --log-file -
最后一个是我的文件夹目录:
我认为问题在于save(),我没有为其分配正确的路径。我已经尝试了一些路径变化,包括/app/static/uploads 和linebot/static/uploads。有什么我想念的吗?
【问题讨论】:
-
"app/static/uploads/testpic.jpg" 是一个相对路径,它将相对于启动应用程序的目录。如果您将 UPLOAD_FOLDER 配置更改为绝对路径,这是否有效?
-
@RossG 感谢您的建议,但仍然收到错误
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\winwo\\pywork\\linebot\\static\\uploads/testpic.jpg'