Hi,
 In our project, we are trying to provide the ability to download a
dataset in a zipped format. We've been transitioning old code to use
web2py, and while I've been able to serve files using web2py elsewhere
(images, for instance), I can't quite figure out how to serve this
particular file. Below is the code I've come up with. When I take out
the code to zip the file, it serves perfectly. Does anyone have
experience with this?

        filename = "%s-backup" % (time.strftime("%Y%m%d-%H%M"))
        raw_data = <a bunch of data>
        stream = cStringIO.StringIO()
        zip_file = zipfile.ZipFile(stream, "w", zipfile.ZIP_DEFLATED, False)
        zip_file.writestr(filename, raw_data)
        response.headers['Content-Type'] = "application/octet-stream"
        response.headers['Content-Disposition'] = "attachment;
filename=%s%s.bin" % (time.strftime("%Y%m%d-%H%M"), filename)
        return response.stream(stream, request=request)

Thanks,
Matt

Reply via email to