2016-07-25 9 views
1

私は(6KB)生成ファイルを提供しようとしています。私が開発にではなく、生産にそれを行うことができます。DjangoからCSVファイルを提供しようとしています

私の見解は、このようなものです:

def download_export_view(request, ref): 
    selection = models.Selection.objects.get(id=ref, user=request.user) 

    filename = ref + '.csv' 
    filepath = os.path.join(EXPORTS_DIR, filename) 
    wrapper = FileWrapper(open(filepath)) 

    response = StreamingHttpResponse(FileWrapper(open(filepath), 8192), 
            content_type=mimetypes.guess_type(filepath)[0]) 
    response['Content-Disposition'] = 'attachment; filename="%s"' % (filename,) 
    response['Content-Length'] = os.path.getsize(filepath) 

    return response 

私は200のステータスおよび(FileNotFoundErrorなど)がない例外を取得し、右ため、ファイルのパスがあることを確信しています、私のブラウザはファイルを取得しません。

+0

私はChromeでテストしましたが、失敗しません。しかし、ダウンロードを開始するには非常に時間がかかります。 – AntonioRomero

+0

最良の方法は、仕事をするためにnginxに任せているようです。そのために私はX-Accel-Redirectを使用しました。 http://stackoverflow.com/questions/28704712/django-nginx-x-accel-redirect-for-protected-files-on-webfaction#answer-28716254のように、これはより論理的です。 – AntonioRomero

答えて

0

私の場合、このスニペットで問題が解決されました。

... 
response = HttpResponse(wrapper, content_type='text/plain') 
response['Content-Disposition'] = 'attachment; filename=%s' % (filename,) 
wrapper.seek(0, os.SEEK_END) 
response['Content-Length'] = wrapper.tell() 
return response 
... 
関連する問題