We can download PDFs in our project. We start out generating the PDF on the server. We read the contents of it into a variable to put it into memory. Then we delete the file on the server (it’s still in memory), and the response is itself the PDF document.
I’ll share the last bit of code that we use: with open(file_and_path, 'rb') as f: pdf_contents = f.read() os.remove(file_and_path) response = HttpResponse(pdf_contents, content_type='application/pdf') response['Content-Disposition'] = "%sfilename=%s" % ('attachment; ' if download else '', filename) return response From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Ruifeng Hu Sent: Tuesday, November 14, 2017 4:56 PM To: Django users Subject: Download a file on Django and delete it after return Hi All, I am now writing a web service which can generate a file and download it automatically for users, but I want to delete it after the file has been downloaded(after return the HttpResponse). What should I do ? Thank You! Ruifeng Hu -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com<mailto:django-users@googlegroups.com>. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/508fcace-efc8-42c0-a667-b760fec4c141%40googlegroups.com<https://groups.google.com/d/msgid/django-users/508fcace-efc8-42c0-a667-b760fec4c141%40googlegroups.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c1c42116948e47ed926312adf4a87853%40ISS1.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.