-1

I am creating a UI. My back end code is in python. Python handlers are called by the javascript(using ajax). I am creating a csv file using python like below.

Results = []
filename = "D:/mydir/testing.csv"
csvfilename = "D:/mydir/testing.csv"
Results.append(["test1","test2","test3"])
with open(filename, 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(Results)
csvfile = open(csvfilename, "r")
self.set_header("Content-Type", "application/csv")   
self.set_header('Content-Disposition', 'attachment; filename="testing.csv"')
tempfile = csvfile.read()
self.write(tempfile)  

Above code is creating file(with type 'Microsoft Office Excel Comma Separated Values File (.csv)').I am calling above handler on click using ajax call like below.

$.get("/filehandler", {startdate:startdate,enddate:enddate,reportname:reportname},function(data)
{
    alert(data);
}); 

Actually my intention is on click over button it should ask for saving 'testing.csv' file. Now i have hard coded array contents, the data for the file should come from python. Is there any way to get solution using below code ?

<a onclick="function()" download>

1 Answer 1

0

Yes, you can link to the script directly:

<a href="path-to-your-hardcoded-python-script">download</a>

In response to you comment, there is no need to use an Ajax call because the parameters are already hard coded. Opening the above link will trigger a download, because the python script already has the download headers set.

1
  • I have to use ajax call for python handlers. ajax response should be downloadable file Commented Mar 5, 2014 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.