I'm trying to make a simple example for sending and receiving files to a mongo db and I can see the images being uploaded correctly to mongodb using mongodbcompass, but I get a type error when I try to retrieve the file and show it. Any help is greatly appreciated!
from flask import Flask, request, url_for
from flask_pymongo import PyMongo
import urllib
app = Flask(__name__)
app.config['MONGO_URI'] = "my-uri-is-here"
mongo = PyMongo(app)
@app.route('/')
def index():
return '''
<form method="post" action="/create" enctype="multipart/form-data">
<input type="text" name="username">
<input type="file" name="profile_image">
<input type="submit">
</form>
'''
@app.route('/create', methods=['POST'])
def create():
if 'profile_image' in request.files:
profile_image = request.files['profile_image']
mongo.save_file(profile_image.filename, profile_image)
mongo.db.imageusers.insert_one({'username' : request.form.get('username'), 'profile_image_filename' : profile_image.filename})
return 'Done!'
@app.route('/file/<filename>')
def file(filename):
return mongo.send_file(filename)
Like I said, the /create route works fine, but when I go to /file/cat.jpeg, I get this error:
Traceback (most recent call last):
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/example.py", line 29, in file
return mongo.send_file(filename)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/flask_pymongo/__init__.py", line 206, in send_file
response.set_etag(fileobj.md5)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/werkzeug/sansio/response.py", line 502, in set_etag
self.headers["ETag"] = quote_etag(etag, weak)
File "/mnt/b/Documents/Miguel/UT/Advanced_Programming_Tools/mongo_images_test_flask/wslenv/lib/python3.8/site-packages/werkzeug/http.py", line 855, in quote_etag
if '"' in etag:
TypeError: argument of type 'NoneType' is not iterable