[英]Flask uploading image FileNotFoundError:
I want to upload a image to Flask server using POST requests.我想使用 POST 请求将图像上传到 Flask 服务器。
here's my FLASK server:这是我的 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)
my show_pic3.html
:我的show_pic3.html
:
<html>
<head>
<title>show_pic</title>
</head>
<body>
<img src="{{ file1 }} ">
</body>
</html>
my requests:我的要求:
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)
The error I keep getting:我不断收到的错误:
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"
my gunicorn:我的独角兽:
gunicorn app_with_handler:app --log-file -
and the last one is my folder directory:最后一个是我的文件夹目录:
I Think the problem is with save()
that I did not assign the right path to it.我认为问题出在save()
上,我没有为其分配正确的路径。 I already tried some variation to path including /app/static/uploads
and linebot/static/uploads
.我已经尝试了一些路径变化,包括/app/static/uploads
和linebot/static/uploads
。 Is there anything I miss?有什么我想念的吗?
I just learned that heroku will not recognize a folder without a file in them, the solution is to add a .keep
file in that empty folder as a placeholder.我刚刚了解到 heroku 将无法识别其中没有文件的文件夹,解决方案是在该空文件夹中添加一个.keep
文件作为占位符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.