Django提供file.tar.gz不压缩
问题描述:
我已经提出了一些file.tar.gz的服务,但是当我下载它时,文件没有压缩。Django提供file.tar.gz不压缩
文件服务器上,在我的应用程序运行时有63 438个字节:
-rw-r--r-- 1 root root 63448 Nov 5 14:13 file.tar.gz
,但是当我下载它,它有716个800字节。
这是我的下载功能:
def download_logs(request):
""" View returning file to download """
file_path = request.GET['filepath']
original_filename = file_path.split("/")[-1]
try:
file_loaded = open(file_path, 'rb')
except IOError as err:
LOG.debug(err)
LOG.debug("File %s does not exist", file_path)
return error_view(request, "IOError", "File no longer exists.")
response = HttpResponse(file_loaded.read(), 'application/x-gzip')
file_loaded.close()
file_type, encoding = mimetypes.guess_type(original_filename)
if file_type is None:
file_type = 'application/octet-stream'
response['Content-Type'] = file_type
response['Content-Length'] = str(os.stat(file_path).st_size)
if encoding is not None:
response['Content-Encoding'] = encoding
# To inspect details for the below code, see http://greenbytes.de/tech/tc2231/
if u'WebKit' in request.META['HTTP_USER_AGENT']:
# Safari 3.0 and Chrome 2.0 accepts UTF-8 encoded string directly.
filename_header = 'filename=%s' % original_filename.encode('utf-8')
elif u'MSIE' in request.META['HTTP_USER_AGENT']:
# IE does not support internationalized filename at all.
# It can only recognize internationalized URL, so we do the trick via routing rules.
filename_header = ''
else:
# For others like Firefox, we follow RFC2231 (encoding extension in HTTP headers).
filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(original_filename.encode('utf-8'))
response['Content-Disposition'] = 'attachment; ' + filename_header
return response
我的图像有是怎么打开的文件的方式有问题,我只是找不到合适的解决方案。
答
由于Klaus D.在评论中说这是编码问题。
当我改变
if encoding is not None:
response['Content-Encoding'] = encoding
其声明正常工作
response['Content-Encoding'] = 'tar'
一切。
设置编码可能会鼓励您的浏览器解压缩文件。要验证使用浏览器调试器来查看下载大小和标题。 –