Download multiple Python Bottle files

I want to upload several files to the Bottle server.

Uploading a single file works well, and by changing the HTML input tag, which is "multiple," the browse button allows you to select multiple files. The download request handler downloads only the last file. How can I upload all files in one go?

The code I'm experimenting with:

    from bottle import route, request, run

    import os

    @route('/fileselect')
    def fileselect():
        return '''
    <form action="/upload" method="post" enctype="multipart/form-data">
      Category:      <input type="text" name="category" />
      Select a file: <input type="file" name="upload" multiple />
      <input type="submit" value="Start upload" />
    </form>
        '''

    @route('/upload', method='POST')
    def do_upload():
        category   = request.forms.get('category')
        upload     = request.files.get('upload')
        print dir(upload)
        name, ext = os.path.splitext(upload.filename)
        if ext not in ('.png','.jpg','.jpeg'):
            return 'File extension not allowed.'

        #save_path = get_save_path_for_category(category)
        save_path = "/home/user/bottlefiles"
        upload.save(save_path) # appends upload.filename automatically
        return 'OK'

    run(host='localhost', port=8080)
+4
source share
1 answer

The mata clause works. You can get a list of downloaded files by calling getall()on request.files.

@route('/upload', method='POST')
def do_upload():
    uploads = request.files.getall('upload')
    for upload in uploads:
        print upload.filename
    return "Found {0} files, did nothing.".format(len(uploads))
+2
source

Source: https://habr.com/ru/post/1599764/


All Articles