0

I want to display an image in a url after sent POST requests with picture to my server, and it will change picture everytime there is a new request comes.

my 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('/show_pic',methods=['POST'])
def show_picture():
    if request.method == 'POST':
        file = request.files['image']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return render_template("show_pic.html", user_img = filename)

my show_pic.html:

<!DOCTYPE html>
<html>
<head>
    <title>show_pic</title>
</head>
<body>
    <img src=" {{url_for('uploads',user_img=filename)}}">
</body>
</html>

my requests:

import requests
pic={'image': open(r'C:\Users\winwo\OneDrive\Documents\testpic.jpg', mode='rb')}
r = requests.post(url, files=pic)

here's the error I'm getting:

2021-03-26T09:47:52.511166+00:00 app[web.1]: [2021-03-26 09:47:52,509] ERROR in app: Exception on /show_pic [POST]
2021-03-26T09:47:52.511203+00:00 app[web.1]: Traceback (most recent call last):
2021-03-26T09:47:52.511204+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
2021-03-26T09:47:52.511204+00:00 app[web.1]:     response = self.full_dispatch_request()
2021-03-26T09:47:52.511205+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-26T09:47:52.511205+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2021-03-26T09:47:52.511206+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-26T09:47:52.511206+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2021-03-26T09:47:52.511206+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2021-03-26T09:47:52.511207+00:00 app[web.1]:     raise value
2021-03-26T09:47:52.511207+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-26T09:47:52.511207+00:00 app[web.1]:     rv = self.dispatch_request()
2021-03-26T09:47:52.511207+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2021-03-26T09:47:52.511208+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2021-03-26T09:47:52.511208+00:00 app[web.1]:   File "/app/app_with_handler.py", line 121, in show_picture
2021-03-26T09:47:52.511209+00:00 app[web.1]:     file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
2021-03-26T09:47:52.511209+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/datastructures.py", line 3066, in save
2021-03-26T09:47:52.511209+00:00 app[web.1]:     dst = open(dst, "wb")
2021-03-26T09:47:52.511214+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: '/app/static/uploads/testpic.jpg'
2021-03-26T09:47:52.512268+00:00 app[web.1]: 10.138.162.166 - - [26/Mar/2021:09:47:52 +0000] "POST /show_pic HTTP/1.1" 500 290 "-" "python-requests/2.25.1"

in the above code, I tried to read and save picture touploads folder. but if there is a way to read and display the picture without having to save it to a folder, that also works for me.

1 Answers1

1

There is a way to render image files on flask without actually saving them on your machine.

Try using data uri.

More information on this can be found on this post.

revmatcher
  • 757
  • 8
  • 17