1

I am trying to return a directory of images from the server to the client. I'm having a hard time understanding how to format my response so that the client is able to distinguish between the files.

What is the best practice way to accomplish what I am doing? my photos are kept in a tmp folder within the application. Here is my utils.py:

def get_multipart_fields():
    final = {}
    for file in os.listdir(os.path.dirname(os.path.realpath(__file__)) + '/tmp'):
        try:
            with open(os.path.dirname(os.path.abspath(__file__)) + '/tmp/' + os.path.join(file), "rb") as image_file:
                encoded_string = base64.b64encode(image_file.read())
                final[os.path.join(file)] = { 'filename': encoded_string }

        except Exception as e:
            print e

    return final.to_string()

and my views.py:

@app.route('/download-files', methods=['GET'])
def gallery():
    client = get_client()
    resource = get_resource()
    download_dir(client, resource, 'test-bucket')

    file_names = get_multipart_fields()

    m = MultipartEncoder(
        fields=file_names
    )

    return Response(json.dumps(m), mimetype=m.content_type)

Where am I going wrong with this logic ? Any help would be greatly appreciated.

snovosel
  • 91
  • 1
  • 2
  • 10
  • What does this currently do that you're not expecting? – OneCricketeer Sep 18 '18 at 00:44
  • @cricket_007 thanks for your message. this returns an extremely long array buffer data to the browser. I'm not sure how to decode that on the front end. I would like to have each file with its base64 sent separately to the client. – snovosel Sep 18 '18 at 01:52
  • If you want files sent separately, you probably shouldn't have a loop and you should request individual photos. If you hosting HTML pages, though, you could just use `` tabs to render all the images asynchronously on the browser renderer – OneCricketeer Sep 18 '18 at 04:26
  • @cricket_007 is there no way to send all files in one response? I feel like looping through a directory and sending each file individually is not efficient. I'm not hosting HTML pages, i am sending this data to a SPA frontend restuflly. – snovosel Sep 18 '18 at 14:25
  • Looks like you're currently sending all files as base64. I don't think you can make servers send multipart downloads, only octet streams – OneCricketeer Sep 18 '18 at 14:48
  • No, you can't send multiple files for download in a single response. HTTP (or MIME rather) would theoretically we able to frame this in a single MIME multipart message, but no real world browser is able to reliably handle this, implementation (if even attempted) varies wildly. Also see: [How to download multiple files with one HTTP request?](https://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request) – Lukas Graf Sep 18 '18 at 23:38

0 Answers0