2

サーバーから自分のマシンに特定のファイルをダウンロードするための簡単な関数を書いています。ファイルは、その ID で表される一意です。ファイルは正しく検索され、ダウンロードは完了しますが、ダウンロードされたファイル (サーバー上のファイルとして名前が付けられています) は空です。私のダウンロード機能は次のようになります。

def download_course(request, id):
    course = Courses.objects.get(pk = id).course
    path_to_file = 'root/cFolder'
    filename = __file__ # Select your file here.                                
    wrapper = FileWrapper(file(filename))
    content_type = mimetypes.guess_type(filename)[0]
    response = HttpResponse(wrapper, content_type = content_type)
    response['Content-Length'] = os.path.getsize(filename)
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(course)

    return response

どこが間違っているのでしょうか? ありがとう!

4

3 に答える 3

2

ここでこの質問に答えました。お役に立てば幸いです。

于 2010-07-09T15:51:35.347 に答える
2

データを送信していないようです (ファイルを開いていません)。

Django には、ファイルを送信するための優れたラッパーがあります (コードはdjangosnippets.orgから取得):

def send_file(request):
    """                                                                         
    Send a file through Django without loading the whole file into              
    memory at once. The FileWrapper will turn the file object into an           
    iterator for chunks of 8KB.                                                 
    """
    filename = __file__ # Select your file here.                                
    wrapper = FileWrapper(file(filename))
    response = HttpResponse(wrapper, content_type='text/plain')
    response['Content-Length'] = os.path.getsize(filename)
    return response

のようなものを使用できますresponse = HttpResponse(FileWrapper(file(path_to_file)), mimetype='application/force-download')

本当に lighttpd を使用している場合 ( 「X-Sendfile」ヘッダーのため)、サーバーと FastCGI の構成を確認する必要があると思います。

于 2010-06-29T14:42:08.153 に答える
1

次のいずれかのアプローチを試してください。

1)GZipMiddlewareを使用している場合は、無効にします。

2) https://code.djangoproject.com/ticket/6027で説明されているdjango / core /servers/basehttp.pyにパッチを適用し ます

于 2012-03-18T16:17:52.373 に答える