简体   繁体   中英

Flask uploading image FileNotFoundError:

I want to upload a image to Flask server using POST requests.

here's my FLASK server:

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 :

<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. I already tried some variation to path including /app/static/uploads and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM