python - Django - HTTPStream of gzip file creaed on the fly -


i used stringio create file object holds xml data, created gzip file file object, having trouble making stream on http django, file size not fixed , sometime can big, why oppted httpstream instead of normale http response. can't alos figure out how send file lenght, because file object not seekable.

thank help, cheers !

here code:

# xml string xml_string = get_xml_cebd(cebds)  # write xml string string io holder cebd_xml_file.write(xml_string)  # flush xml file buffer cebd_xml_file.flush()  # create gzip file handler, stringio in fileobj gzip_handler = gzip.gzipfile(     fileobj=cebd_xml_file,     filename=base_file_name + '.xml',     mode='wb' )  # write xml data gziped file gzip_handler.write(cebd_xml_file.getvalue()) gzip_handler.flush()  # generate response using file warpper, content type: x-gzip # since file can big, best use streaminghttpresponse # way can garantee file sent entirely response = streaminghttpresponse(     gzip_handler,     content_type='application/x-gzip' )  # add content disposition browser download file response['content-disposition'] = ('attachment; filename=' +     base_file_name + '.xml.gz')  # content size gzip_handler.seek(0, os.seek_end) response['content-length'] = gzip_handler.tell() 

i found solution both problems, handler supposed passed httpstream stringio one, not gzip handler, stringio handler seekable way can check size of data after gzipped, trick call close method on gzip handler add crc32 , size gzip file otherwise data sent 0, stringio don't call close method, because httpstream need handler open stream data, garbage collector close after stream done.

this final code:

# use cstringio create file on fly cebd_xml_file = stringio.stringio()  # create file name ... base_file_name = "cebd"  # xml string xml_string = get_xml_cebd(cebds)  # create gzip file handler, stringio in fileobj gzip_handler = gzip.gzipfile(     fileobj=cebd_xml_file,     filename=base_file_name + '.xml',     mode='wb' )  # write xml data gziped file gzip_handler.write(xml_string)  # flush data gzip_handler.flush()  # close gzip handler, close method add crc32 , size gzip_handler.close()  # generate response using file warpper, content type: x-gzip # since file can big, best use streaminghttpresponse # way can garantee file sent entirely response = streaminghttpresponse(     cebd_xml_file.getvalue(),     content_type='application/x-gzip' )  # add content disposition browser download file, don't use mime type ! response['content-disposition'] = ('attachment; filename=' +     base_file_name + '.xml.gz')  # content size cebd_xml_file.seek(0, os.seek_end) response['content-length'] = cebd_xml_file.tell()  # send response request, don't close stringio handler ! return response 

cheers, hope can anyone.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -