Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. def upload(request):
  2. if request.POST:
  3. form = FileForm(request.POST, request.FILES)
  4. if form.is_valid():
  5. form.save()
  6. return render_to_response('project/upload_successful.html')
  7. else:
  8. form = FileForm()
  9. args = {}
  10. args.update(csrf(request))
  11. args['form'] = form
  12.  
  13. return render_to_response('project/create.html', args)
  14.  
  15. <a href="/project/download"> Download Document </a>
  16.  
  17. urlpatterns = [
  18.  
  19. url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
  20. template_name="project/project.html")),
  21. url(r'^(?P<pk>d+)$', DetailView.as_view(model=Post, template_name="project/post.html")),
  22. url(r'^upload/$', upload),
  23. url(r'^download/(?P<path>.*)$', serve, {'document root': settings.MEDIA_ROOT}),
  24.  
  25. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  26.  
  27. def download(request):
  28. file_name = #get the filename of desired excel file
  29. path_to_file = #get the path of desired excel file
  30. response = HttpResponse(mimetype='application/force-download')
  31. response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
  32. response['X-Sendfile'] = smart_str(path_to_file)
  33. return response
  34.  
  35. import os
  36. from django.conf import settings
  37. from django.http import HttpResponse, Http404
  38.  
  39. def download(request, path):
  40. file_path = os.path.join(settings.MEDIA_ROOT, path)
  41. if os.path.exists(file_path):
  42. with open(file_path, 'rb') as fh:
  43. response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
  44. response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
  45. return response
  46. raise Http404
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement