I'm trying to insert PDF files into a MongoDB database. The files are small enough (<16 MegaBytes) so I don't think I need to add the complexity of GridFS (even though it looks pretty easy to use based on the tutorials I've seen). How can I do this using flask_pymongo
(or even a basic example using pymongo
would be great).
Here's what I have so far but I'm getting the following error:
bson.errors.InvalidStringData: strings in documents must be valid UTF-8
flask_app.py:
from flask import Flask, render_template_request
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'records'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/records'
mongo = PyMongo(app)
@app.route('/', methods=['GET', 'POST'])
def upload():
if request.method = 'POST':
files_collection = mongo.db.files_collection # connect to mongodb collection
input_file = request.files['input_file'] # get file from front-end
files_collection.insert_one({'data': input_file.read() }) # error occurs here
return 'File uploaded'
return render_template('index.html')
index.html:
<form method='POST' action="{{ url_for('upload') }}" enctype='multipart/form-data'>
<input type='file' name='input_file'>
<input type='submit' value='Upload'>
</form>
Seems like I just need to convert the data to the proper data type before entering it into mongodb, which appears to be the binData
type based on this answer here