django - Python zipfile not being delivered to user -
update: got zip file created, response still isn't working properly. here's updated code.
def index(request): if request.is_ajax(): uploaded = request.body newstring = "{\"testdata\": [" + uploaded + "]}" data = json.loads(newstring) num_lines = sum(len(v) v in data.itervalues()) testing(data, num_lines) zip_subdir = 'testapi' zip_filename = '%s.zip' % zip_subdir zf = z.zipfile(zip_filename, mode='w') zf.write('test.xlsx') zf.close() zip_file = open(zip_filename, 'r') response = httpresponse(zip_file, content_type='application/x-zip-compressed') response['content-disposition'] = 'attachment; filename=%s' % zip_filename return response
just make sure, deleted zip file , excel spreadsheet, , after running program, both created properly. excel spreadsheet populated data, , copy of put generated zip archive. however, still not getting sent user. idea might going wrong response? no errors showing up, zip file isn't being delivered.
original question below.
i'm trying django application write files zip , send them user. part of code have in view:
def index(request): if request.is_ajax(): uploaded = request.body newstring = "{\"testdata\": [" + uploaded + "]}" data = json.loads(newstring) num_lines = sum(len(v) v in data.itervalues()) testing(data, num_lines) zip_subdir = 'testapi' zip_filename = '%s.zip' % zip_subdir s = stringio.stringio() zf = z.zipfile(s, 'w') zf.write('test.xlsx') zf.close() response = httpresponse(s.getvalue(), content_type='application/x-zip-compressed') response['content-disposition'] = 'attachment; filename=%s' % zip_filename return response
essentially, user presses button, ajax request made json external api, , json data sent server , put excel spreadsheet (called 'test.xlsx'). of works properly.
the issue zip archive ('testapi.zip') not being created properly. what's odd it's same code used when user uploaded json dump via form (rather use api), , worked before. however, won't create zip file. additionally, if manually place zip file called 'testapi.zip' in proper directory, response doesn't work (again, used work before - in prior version of app, zip file sent user , automatically downloaded). no errors returned, nothing happens.
any idea might going on?
the issue seems in following code -
s = stringio.stringio() zf = z.zipfile(s, 'w')
you seem openning zipfile in buffer , instead of creating zipfile in location name zip_filename
.
try openning file using path/name of file, instead of using stringio.stringio()
. -
zf = z.zipfile(zip_filename,'w')
edit :
try using absolute paths instead of relative path. -
zip_subdir = '/path/to/zip/file/testapi'
Comments
Post a Comment