Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use pyramid.response.FileIter
    primarykey
    data
    text
    <p>I have the following view code that attempts to "stream" a zipfile to the client for download:</p> <pre><code>import os import zipfile import tempfile from pyramid.response import FileIter def zipper(request): _temp_path = request.registry.settings['_temp'] tmpfile = tempfile.NamedTemporaryFile('w', dir=_temp_path, delete=True) tmpfile_path = tmpfile.name ## creating zipfile and adding files z = zipfile.ZipFile(tmpfile_path, "w") z.write('somefile1.txt') z.write('somefile2.txt') z.close() ## renaming the zipfile new_zip_path = _temp_path + '/somefilegroup.zip' os.rename(tmpfile_path, new_zip_path) ## re-opening the zipfile with new name z = zipfile.ZipFile(new_zip_path, 'r') response = FileIter(z.fp) return response </code></pre> <p>However, this is the Response I get in the browser:</p> <p><strong><code>Could not convert return value of the view callable function newsite.static.zipper into a response object. The value returned was .</code></strong></p> <p>I suppose I am not using FileIter correctly.</p> <hr> <h1>UPDATE:</h1> <p>Since updating with Michael Merickel's suggestions, the FileIter function is working correctly. However, still lingering is a MIME type error that appears on the client (browser): <code>Resource interpreted as Document but transferred with MIME type application/zip: "http://newsite.local:6543/zipper?data=%7B%22ids%22%3A%5B6%2C7%5D%7D"</code></p> <p>To better illustrate the issue, I have included a tiny <code>.py</code> and <code>.pt</code> file on Github: <strong><a href="https://github.com/thapar/zipper-fix" rel="nofollow">https://github.com/thapar/zipper-fix</a></strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload