Download a file on Django and delete it after return

1,856 views
Skip to first unread message

Ruifeng Hu

unread,
Nov 14, 2017, 5:56:09 PM11/14/17
to Django users
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

 

Juan Hernandez

unread,
Nov 14, 2017, 6:05:01 PM11/14/17
to Django users
well, we could see the use case to see the correct approach but, what comes to mind is using try/except/finally

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
Nov 14, 2017, 6:05:20 PM11/14/17
to django...@googlegroups.com

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

--

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...@googlegroups.com.

Ruifeng Hu

unread,
Nov 14, 2017, 6:23:22 PM11/14/17
to Django users
Many thanks for your reply ! ! !


On Tuesday, November 14, 2017 at 5:05:20 PM UTC-6, Matthew Pava wrote:

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...@googlegroups.com [mailto:django...@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...@googlegroups.com.

To post to this group, send email to djang...@googlegroups.com.

James Schneider

unread,
Nov 14, 2017, 7:38:59 PM11/14/17
to django...@googlegroups.com


On Nov 14, 2017 3:04 PM, "Matthew Pava" <Matthe...@iss.com> wrote:

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.


Note that this strategy has two potentially negative side effects:

1. The original file is no longer available for a second attempt to download in the event the first attempt fails. This may be fine if the file is generated every time as part of the request.

2. Loading the file in to memory is usually find for smaller sites, but high traffic sites can run in to memory resource issues.

You can also have a cron job delete files every so often based on their age, rather than deleting them as part of the immediate response cycle. That would save on memory, if that's a concern.

-James
Reply all
Reply to author
Forward
0 new messages