def get_file(request):
file object = ....
return file_object
from django.core.servers.basehttp import FileWrapper
def get_file(request):
filename = '...'
content_type = 'application/vnd.ms-excel'
file_path = os.path.join(store_path, filename)
response = HttpResponse(FileWrapper(file(file_path)), content_type=content_type)
response['Content-Disposition'] = 'attachment; filename=%s' % (
filename.encode('utf-8') if isinstance(filename, unicode) else filename,
)
response['Content-Length'] = os.path.getsize(path)
return response
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
def get_document(request, file_link):
document = get_object_or_None(Document, link=file_link, users=request.user.id)
if document:
return serve(request,
path=document.document.name,
document_root=settings.USERS_DOCUMENT_ROOT)
return HttpResponse('Error!')