How to download a file in Django | Making a large file downloadable from a URL in Django | How to serve Large files in Django | Downloading a file from the Django project directory
Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform. One way is to download the file and then upload it to another platform or give it to the person manually, which is not a good approach.
This is a simple thing in Django. You can serve any file from any directory (i.e. inside the project directory) using a URL in Django. Just follow the steps shown below and you will find that some line of code will help you to do that.
If the file is binary:
Add the following URL in URL patterns.
#In urls.py
path('download/',views.download,name="download"),
Now set the function in views.py i.e. download() as mapped in urls.py
#In views.py
from django.http import FileResponse
import os
def download(request):
file_path = os.path.join(BASE_DIR, 'myfile.zip')
response = FileResponse(open(file_path, 'rb'))
return response
So now if someone visits "www.example.com/download" the downloading of file i.e "myfile.zip" will start.
Note that if the file is not binary then you have to use 'r' instead of 'rb' in your code.
If the file is a text/UTF-8 file
If you only want to show the text content of the file then you can use the following code:
#In views.py
from django.http import FileResponse
import os
def download(request):
file_path = os.path.join(BASE_DIR, 'requirement.txt')
f = open(file_path, 'r')
file_content = f.read()
f.close()
return HttpResponse(file_content, content_type="text/javascript")
In this case, the text content of the file will be server in the browser or client device.
Now, if the file is stored in database models/fields then you can do something like this:
#In views.py
from django.http import FileResponse
import os
def download(request):
obj=mymodel.objects.get(id=someid)
response = FileResponse(open(obj.fileField.path, 'rb'))
return response
Recent Articles
One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...
- 2023-07-12Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....
- 2023-07-12A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing....
- 2023-07-12After reading this article you will never ever search for web development guide neither on Google nor on YouTube....
- 2023-07-12I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....
- 2023-07-12In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....
- 2023-07-12In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....
- 2023-07-12