def _tarFiles(filepaths):
print "create tar file from all files in file list and save to temp working dir. returns tarfile path "
try:
savePathDir = settings.TAR_FILE_TARGET_DIRECTORY
if not os.path.exists(savePathDir):
os.makedirs(savePathDir)
tarredfiles = tarfile.open(settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar',mode='w')
for f in filepaths:
tarredfiles.add(f)
tarredfiles.close()
return ("Ok", settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar')
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
def sendFiles(files):
try:
result, tarfilename = _tarFiles(files)
if result == 'Ok':
try:
print tarfilename
wrapper = FileWrapper(file(tarfilename))
response = HttpResponse(wrapper, content_type='application/x-tar') #zip,avi,png,jpeg, etc...
response['Content-Disposition'] = 'attachment; filename=' + tarfilename#tarredfiles.name #eg. myfile.zip
response['Content-Length'] = str(os.path.getsize(tarfilename))
return ("Ok",response)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
else:
return (result,None)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " +e.message,None)
tarfilename is the complete path to the file.
The content-length looks right (comparing actual file to getsize).
Works on a mac running runserver. Returns a partial file on windows running runserver. Or completely empty file if I step through on windows.
The target directory and filename generated is “tarred_files/responsefiles.tar”
the file size is 90K and the os.path.getsize returned is 92160
What am I doing that would cause an empty file to be downloaded?
For windows, you need to add “rb” to file.
like so:
Also, Content-Length should use a integer, not a string
like so: