6

I have an Excel Spreadsheet (.xls) which i want the users to be able to download it from my webpage. Maybe a 'download' button on the page and when users click on it, the file will be downloaded.

May i know how can this be done? Code snippets and references will be appreciated.

Naveed
  • 40,370
  • 32
  • 94
  • 130
Lloydworth
  • 733
  • 6
  • 20
  • 37
  • Why don't you just link to the file? Preferably, you should be serving it with the correct MIME type, but it should work regardless... – Joseph Silber Jan 06 '12 at 02:31

3 Answers3

12

You can use simple html links:

link:

<a href='/path/to/excel/file.xls' target="_blank">Download</a>

button:

<form>
<input type="button" value="Download" onClick="window.location.href='/path/to/excel/file.xls'">
</form>
Naveed
  • 40,370
  • 32
  • 94
  • 130
  • is it possible to use a relative link when trying to download a file locally? using `click` tries to load a file at `file:///C:/docs/my_file.xlsx`, rather than looking in the local website directory. – user1063287 Feb 15 '20 at 05:48
1

How about just referring to the file through an anchor?

<a href="path-to-file.xls">Download</a>
Christofer Eliasson
  • 31,342
  • 6
  • 71
  • 100
  • I am using anchor but it's not downloading the file :( https://stackoverflow.com/questions/46876439/excel-file-download-failed-in-chrome-using-php-yii2 – Moeez Oct 22 '17 at 16:29
0

I think this should be fine too:

<a href="path-to-file" download="abc.xls">Download</a>

download: name of file to be downloaded as.