0

I'm trying to return a zip file with HttpResponse, using StringIO() because i'm not storing in DB or Harddrive. My issue is that my response is returning 200 when i request the file, but the OS never ask me if i want to save the file, or the file is never saved. i think that the browser is reciving the file because i have seen on the Network Activity (inspect panel) and it says than a 6.4 MB file type zip is returned. I'm taking a .step file (text file) from a DB's url, extracting the content, zipping and returning, that's all.

this my code:

def function(request, url_file = None):

    #retrieving info
    name_file = url_file.split('/')[-1]
    file_content = urllib2.urlopen(url_file).read()
    stream_content = StringIO(file_content)
    upload_name = name_file.split('.')[0]

    # Create a new stream and write to it

    write_stream = StringIO()
    zip_file = ZipFile(write_stream, "w")
    try:
        zip_file.writestr(name_file, stream_content.getvalue().encode('utf-8'))
    except:
        zip_file.writestr(name_file, stream_content.getvalue().encode('utf-8', 'ignore'))

    zip_file.close()

    response = HttpResponse(write_stream.getvalue(), mimetype="application/x-zip-compressed")
    response['Content-Disposition'] = 'attachment; filename=%s.zip' % upload_name
    response['Content-Language'] = 'en'
    response['Content-Length'] = write_stream.tell()

    return response

0 Answers0