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 是文件的完整路径。内容长度看起来正确(将实际文件与 getsize 进行比较)。
在运行 runserver 的 mac 上工作。返回运行 runserver 的 Windows 上的部分文件。如果我在 Windows 上单步执行,或者完全为空文件。
生成的目标目录和文件名是“tarred_files/responsefiles.tar”,文件大小为 90K,返回的 os.path.getsize 为 92160
我在做什么会导致下载一个空文件?