T
T
tayoboc2017-02-21 14:53:30
Python
tayoboc, 2017-02-21 14:53:30

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)

Prompt in what direction to dig.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-02-21
@tayoboc

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 question

Ask a Question

731 491 924 answers to any question