Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

620
Views
Django - download file without reloading the page

The flow looks like that:

  1. user is filling the form
  2. form is passed to the server by ajax
  3. form is saved to db, then the pdf with the form data is created and saved in the app folder (probably a bad move here...)
  4. ajax success causes the page to append a button 'Download' with value equal to current pdf's name so button 'Download' appears to the user
  5. If user presses the button the very pdf that was just saved is gonna download.
  6. Refreshing the page makes the button disappear.

I've got stuck on point 5. I have created another ajax (to avoid reloading the page) bound to the Download button. It correctly asks the server to look for the file, creates a django File object: pdf_file = File(open(file_path, 'rb')) and creates a HttpResponse with file, and content_type='application/pdf' or 'application/download'. response['Content-Disposition'] is attachment.

Then the ajax returns response - only it does not. Server raises no error but ajax error function is called.

I've read that downloading with ajax is not possible. Could you help me a bit to get it straight? If above snippets are not clear, I shall provide more code. Python 3.5, Django 1.10

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Do like

window.location.href = "/url/to/downloadfile/"

in javascript after success of posting form. OR

#html
<button onclick="myfiledownload()">Download</download>
#javascript
function myfiledownload(){
    window.location.href = "/url/to/downloadfile/"
}
about 4 years ago · Santiago Trujillo Report

0

Instead of using ajax to download the file bind the button to a download link where the file may be hosted

https://www.mywebsite/download/?fileid=3247023

You should at least seperate your file in a media root Note: in production you will have to use a cdn to host your static files

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!