1

I have multiple json and csv files under the static folder of Web2Py that I'd like to download from the server to local client with a web2py controller and response.stream. The following controller code works:

def my_download():
    directory = '2e6d2ba1-52d7-4927-b91d-c7568b25314d/'
    Cat = 'problems'

    path=os.path.join(request.folder,'static', directory + Cat + '.json')
    return response.stream(open(path,'rb'), attachment=True, filename=Cat)

How to loop thru the other files in directory, which have Cat = 'foo' and Cat ='bar', etc. and stream / dl all to client?

After the first return response.stream and successful run, controller doesn't have focus anymore, so it cannot continue execute another return response.stream or any other statement for that matter (like deleting the files, for example).

Toren
  • 442
  • 3
  • 13
  • Have you considered using a tar file to group all the files in your directory? Check out this link: https://stackoverflow.com/questions/2032403/how-to-create-full-compressed-tar-file-using-python – Reboot_87 Sep 09 '20 at 17:21
  • Sure, thanks. But still I have a problem of executing code AFTER the return response.stream (such as deleting the files after dl) .... – Toren Sep 09 '20 at 17:50
  • Is it possible to create the tar file before starting the response.stream? meaning, inside the controller? – Reboot_87 Sep 09 '20 at 17:57

1 Answers1

0

use jquery events and use ajax call along with argument(file names or anthing) to call controller function. it works like loops.

Bkamath
  • 23
  • 8