Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def upload(request):
- if request.POST:
- form = FileForm(request.POST, request.FILES)
- if form.is_valid():
- form.save()
- return render_to_response('project/upload_successful.html')
- else:
- form = FileForm()
- args = {}
- args.update(csrf(request))
- args['form'] = form
- return render_to_response('project/create.html', args)
- <a href="/project/download"> Download Document </a>
- urlpatterns = [
- url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
- template_name="project/project.html")),
- url(r'^(?P<pk>d+)$', DetailView.as_view(model=Post, template_name="project/post.html")),
- url(r'^upload/$', upload),
- url(r'^download/(?P<path>.*)$', serve, {'document root': settings.MEDIA_ROOT}),
- ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- def download(request):
- file_name = #get the filename of desired excel file
- path_to_file = #get the path of desired excel file
- response = HttpResponse(mimetype='application/force-download')
- response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
- response['X-Sendfile'] = smart_str(path_to_file)
- return response
- import os
- from django.conf import settings
- from django.http import HttpResponse, Http404
- def download(request, path):
- file_path = os.path.join(settings.MEDIA_ROOT, path)
- if os.path.exists(file_path):
- with open(file_path, 'rb') as fh:
- response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
- response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
- return response
- raise Http404
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement