Tuesday, November 3, 2009

Javascript to download instead of open a file

You can use Javascript to setup a link to download a file instead of opening it. Example:

<a href="#" onclick="window.open('mysite/myfolder/test.xlsx', 'Download');">Download my file</a>

For those that might have Javascript turned off, include an alternate link that has href directly going to the file's URL:

<a href="mysite/myfolder/test.xlsx">Alternate Link</a>

There's probably a combined solution that's more SEO-friendly (don't want the "#" as the href). I tried it but ended up with two popup windows. Need some research on that.

No comments:

Post a Comment