0

I am creating a file download functionality on link click from admin panel in django. I am using FileField for storing the files. For the download purpose I researched and found help on stackoverflow. After using that help, I have the following code for file download (with some minor changes of my own).

def pdf_download(request):
    #print("request: ", request.META["PATH_INFO"])
    a = request.META["PATH_INFO"]
    #print(type(a))
    a = a.split("/")
    a = a[-1]
    #print(a)
    #print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    with open(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+"\\router_specifications\\"+a ,"rb") as pdf:
        #Here router_specifications is the directory on local storage where the uploaded files are being stored.
        response = HttpResponse(pdf.read()) #can add ', content_type = "application/pdf" as a specific pdf parameter'
        response["Content-Disposition"] = "attachment; filename ="+a
        pdf.close()
        return response

Now, when I this code runs in my laptop, the file is downloaded automatically. but, when I switch to some other laptop, it asks me where should I save the file i.e. it's not automatically getting downloaded. What changes should I do so that the file automatically gets downloaded without asking for manual save. Requesting help at the earliest.

Aishwary Shukla
  • 450
  • 1
  • 7
  • 21
  • That would depend how you've configured your browser. In your browser settings, there is normally an option "Save file to Downloads" and a second option "Always ask you where to save files". It sounds as though on your laptop the first option is selected, whereas on the other laptop the second option is selected. – Will Keeling Jun 12 '18 at 07:59

1 Answers1

1

You can try adding the following content_type:

content_type='application/force-download'

Manu mathew
  • 859
  • 8
  • 25