2016-06-07 86 views
0

我已經提出了一些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 

我的圖像有是怎麼打開的文件的方式有問題,我只是找不到合適的解決方案。

+2

設置編碼可能會鼓勵您的瀏覽器解壓縮文件。要驗證使用瀏覽器調試器來查看下載大小和標題。 –

回答

0

由於Klaus D.在評論中說這是編碼問題。

當我改變

if encoding is not None: 
    response['Content-Encoding'] = encoding 

其聲明正常工作

response['Content-Encoding'] = 'tar' 

一切。