Answer the question
In order to leave comments, you need to log in
How to store files in a database in Bottle and SQLite?
Hello!
Now I'm saving files to disk with this code.
But I want to store them in a database with other data that is entered into the form manually.
Form data is stored in SQLite just fine.
But how to organize the storage of files (document scans) and getting links to them is unclear.
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.pdf', '.jpg', '.jpeg'):
return "File extension not allowed."
save_path = ".../tmp/{category}".format(category=category)
if not os.path.exists(save_path):
os.makedirs(save_path)
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "File successfully saved to '{0}'.".format(save_path)
Answer the question
In order to leave comments, you need to log in
Here it is not necessary to store only files in a DB.
And there is os.path.join to generate paths
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question