2011-09-12 3 views
2

はジャンゴ1.3を使用してブラウザからファイルをダウンロードしたコードであるとApache 2.2 mod_xsendfileとDjangoの添付ファイル全体をダウンロードすることができませんでし

@login_required 
def sendfile(request, productid):  
    path = settings.RESOURCES_DIR 
    filepath = os.path.join('C:/workspace/y/src/y/media/audio/','sleep_away.mp3') 
    print "filepath",filepath 
    filename = 'sleep_away.mp3' # Select your file here. 
    print "Within sendfile size", os.path.getsize(filepath) 
    wrapper = FileWrapper(open(filepath,'r'))  
    content_type = mimetypes.guess_type(filename)[0]  
    response = HttpResponse(wrapper, content_type = content_type) 
    print "Within wrapper" 
    from django.utils.encoding import smart_str 
    response['X-Sendfile'] = smart_str(filepath) 
    response['Content-Length'] = os.path.getsize(filepath) 
    from django.utils.encoding import smart_str  
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(filename)  
    return response 

コンソールはsendfileの中で適切なサイズ ある以下のファイルサイズを示していサイズ4842585

ファイルをダウンロード/保存すると、107 KB、つまり109,787バイトと表示されます。間違っています。なぜ完全なファイルをダウンロードしていないのですか?

答えて

2

あなたの新しいdjangoまたはpythonを考えてみましょう。 methodの先頭にimportステートメントを配置してください。インポートすると、使用するたびにインポートする必要がなくなります。ウィンドウでは、"rb"(バイナリを読む)を使用して、テキストファイル以外のものを提供する必要があります。メソッド名や言語の他のキーワードと衝突する可能性のある変数名を使用しないでください。あなたの方法は

+0

うまく機能のおかげPannu ... ...私は次の行を編集していた:応答=のHttpResponse(file_wrapperに応答=のHttpResponseを(CONTENT_TYPE = file_mimetype、file_wrapper) 、content_type = file_mimetype)。 – Anjali

+0

私はうれしいです:)しかし、 'content_type'を' ** kwargs'引数の後に置くのは本当に問題ありません。解決策があなたのために働いていれば、それが開かれたままではないように、答えとしてマークしてください。 :) – Pannu

0

あなたはジャンゴ・プライベート・ファイルのプロジェクトを見ている可能性が役に立てば幸い

@login_required 
def sendfile(request, productid): 
    from django.utils.encoding import smart_str 

    ##set path and filename 
    resource_path = settings.RESOURCES_DIR # resource dir ie /workspace/y/src/y/media 
    filename = "sleep_away.mp3" #file to be served 

    ##add it to os.path 
    filepath = os.path.join(resource_path,"audio",filename) 
    print "complete file path: ", filepath  

    ##filewrapper to server in size of 8kb each until whole file is served 
    file_wrapper = FileWrapper(file(filepath,'rb')) ##windows needs rb (read binary) for non text files 

    ##get file mimetype 
    file_mimetype = mimetypes.guess_type(filepath) 

    ##create response with file_mimetype and file_wrapper  
    response = HttpResponse(content_type=file_mimetype, file_wrapper) 

    ##set X-sendfile header with filepath 
    response['X-Sendfile'] = filepath ##no need for smart_str here. 

    ##get filesize 
    print "sendfile size", os.stat(filepath).st_size 
    response['Content-Length'] = os.stat(filepath).st_size ##set content length  
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(filename) ##set disposition  

    return response ## all done, hurray!! return response :) 

このようにする必要があります。それを自分でテストしていないが、それは約束しているように見える。ドキュメントへのリンク - > http://readthedocs.org/docs/django-private-files/en/latest/usage.html

歓声

関連する問題