Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:32:45+00:00 2026-05-24T04:32:45+00:00

I am jQuery newbie and I have been trying to set up a file

  • 0

I am jQuery newbie and I have been trying to set up a file download dialog window without any success. If and when the dialog window is loaded, the user should have the option to download the dynamically generated file. I am not able to set up a dialog window.

On debugging, I can see that a valid http response is generated. Generated content disposition data is given below:

 attachment; filename=foo.csv

Use Case:

My application is a Django web app used to display the data fetched from the database on a django template. I want to provide the ability to download the displayed data in a csv format when required if the user clicks on a button with text ‘Export To Csv’

Code

Javascript/Html

/**
 * Creates a file to be downloaded upon clicking a button.
 */
 $('button[id*="ExportToCsv"]').click(function() {
    var report_type = $(this).attr('id').split('ExportToCsv')[0];
    // var report_date = '{{ report_date }}'.split('-');
    $.ajax({
        url: '/reports/' + report_type + '/export_to_csv/',
        type: 'POST',
        mimeType: 'text/csv',
        data: {'report_date': '{{ report_date }}'},
        success: function(data) {
            return data;
        }
    });
 });

Html:

<button id = "ExportToCsv">Export To Csv</button>

Django:

class CsvOutputResponse(object):
  """Handles a csv file attachment object.

  Attributes:
    filename: String name of the csv file.
    response: HttpResponse object.
    writer: Csv writer object.
  """

def __init__(self, filename):
  """Initalizes the CsvOutputResponse class.

   Args:
     filename: String name of the csv file.
   """
   self.filename = filename
   self.response = self._InitializeResponse()
   self.writer = csv.writer(self.response)

def _InitializeResponse(self):
  """Initialize a csv HttpResponse object.

  Returns:
    HttpResponse object.
  """
  response = django_dep.HttpResponse(mimetype='text/csv')
  response['Content-Disposition'] = (
      'attachment; filename=%s.csv' % self.filename)
  return response

def WriteRow(self, content):
  """Write a single row to the csv file.

  Args:
    content: List of strings of csv field values.
  """
  self.writer.writerow(content)

def WriteRows(self, content):
  """Write multiple row to the csv file.

  Args:
    content: List of lists of strings of csv field values.
  """
  self.writer.writerows(content)

def GetCsvResponse(self):
  """Get the csv HttpResponse object.

  Returns:
    content: HttpResponse object.
  """
  return self.response

urls.py

(r'^reports/(?P<report_type>\w+)/export_to_csv/$',
 'myproject.myapp.views.ExportTab')

views.py

def ExportTab(request, report_type):
  """Generates a file to be exported and made available for download.

  Args:
    request: HttpRequest object.
    report_type: String type of report to be generated.

  Returns:
    HttpResponse object.
  """
  report_date = request.POST['report_date']
  db = database.Database()
  if report_type == 'Trailing':
    reports = containers.GetTrailingReports()
  elif report_type == 'Ytd':
    reports = containers.GetYtdReports()
  return CsvOutputResponse('foo.txt').writeRows(reports).GetCsvResponse()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:32:46+00:00Added an answer on May 24, 2026 at 4:32 am

    Instead of performing the POST in AJAX, have the browser navigate to the view naturally. The browser will then prompt for the download.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a total JQuery newbie and I have been researching this for the
I am a jQuery newbie. I have a core js file which will not
I am a jquery newbie and have been adding alert timestamps into my html
I'm a jQuery newbie, and I have trouble with a little script I created.
I am newbie to jQuery. In netbeans I have jquery-1.7.2.js where all HTML files
I'm a newbie to jquery, but have managed to make a show-function and hover
I am a newbie at Javascript and Jquery. I have a form that I
Jquery newbie here. In a wrapped element set, is there a way to check
Relative jQuery newbie: I want to display a set of checkboxes, of which the
I'm a newbie trying to learn how to build a jQuery Mobile App and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.