I am creating a CKAN plugin to allow users to select and download multiple resources (files) from a database server. I created a flask view function with some Python code that should create a zip file with the requested resources, but the creation fails with "no such file or directory" error even though I give the absolute path.
The code is based on this, this, and this post, and to solve the current question I have already checked here and here with not much success.
The code is as follows:
import zipfile
import io
def download_multiple_resources():
if request.method == "POST":
memory_file = io.BytesIO()
with zipfile.ZipFile(memory_file, "w", zipfile.ZIP_STORED) as zf:
for res in request.form.values():
zf.write(str(res), 'download.zip')
return send_file(memory_file, mimetype='application/zip', as_attachment=True, attachment_filename='download.zip')
The resources are selected by the user via a form, which posts the request.
The form submits the full path of the requested file, so if the user selected to download testfile.txt
, in the code above the value of res
is something like http://localhost:5000/dataset/6192bb3b-6c4a/resource/dbe39d59-b938/download/testfile.txt
. (currently testing on a server created in a docker container on my machine)
Note that copy-pasting the above path in my browser lets me access the file without any issue.
However, the zf.write
command fails with "no such file or directory" error.
Any idea for what is going wrong?