Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  60] [ 6]  / answers: 1 / hits: 20196  / 12 Years ago, sat, march 2, 2013, 12:00:00

I hava a backbonejs view containing a button saying download pdf. I also have the url available where the pdf can be found. I want that when user clicks on the button, the pdf file gets downloaded. Is it possible?



EDIT: My view code in backbone.js



savendownload: function () {
this.$(#saveanddownload).button('loading');

var that = this;
var formData = this.fetchData();
if (formData) window.invoices.create({
buyer: formData.buyer,
items: formData.items,
company: formData.company,
action: savendownload
}, {
wait: true,
success: function (model) {
var data = model.toJSON();

var filename = data.filename;
//download code here
}
});
}

More From » jquery

 Answers
8

Don't use a button, use a link and set the href attribute to the URL of your PDF file. The browser will handle the file download for you, honoring the user's browser preferences.



<a href=your/file.pdf />


If you need the link to look like a button, you can style it using CSS. See for example this SO thread.



Edit: AFAIK, you can't reliably initialize a file download from javascript. What you can do is to open a new window/tab with your pdf URL:



window.open(http://domain.com/document.pdf,'_blank');


But the user's browser can block the new window from being created. You might want to simply generate a download link:



$('<a>Click here to download PDF</a>').attr('href', filename).appendTo(that.$el);


And have the user click the link to initiate the file download.


[#79885] Friday, March 1, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennonr

Total Points: 124
Total Questions: 101
Total Answers: 101

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;