Return ZIP file over AJAX

I need to download files, which is triggered via AJAX. My code retrieves images from URLs in a temp folder, and creates an in memory zip of the the files in this temp folder. I want to push the file path to the client. How would I do it?

Here is my zipping code:

#This function creates a temp folder, downloads images in it and returns the path
temp_dir = retrieve_images(file_paths)

in_memory_loc = io.BytesIO()

zipf = zipfile.ZipFile(, 'w')
zipdir(temp_dir, zipf)
zipf.close()

#remove the temp directory
shutil.rmtree(temp_dir)

in_memory_loc.seek(0)

#output the zip file to the browser
return send_file(in_memory_loc, attachment_filename='site_images.zip', as_attachment=False)

This works when I use the traditional form submit way. But I need to return this over AJAX. Returning a file over AJAX seems impossible, hence I want to return the zip file path. How would I do it?

Answers 1

  • Create a url to that resource and send it back to the ajax, trigger the download with window.open('url'); in the success function

    You have the name of the temp directory and the name of the file you have all that you need to build a url


Related Articles