'Download a file in HTML

Hi I want to have the option on my site for the user to download a CSV file. I have used the code below

    <input type="button" value="Download as CSV file" onclick="window.location.href='call_log.csv'  " />

This does work but when the button is clicked the file is opened in another tab on my browser, What I want to happen is a download straight to the users default download folder

Can anyone help me please ?

Thanks



Solution 1:[1]

I'm doing the same for my portfolio and I found a very simple way to download my cv as pdf from my website's local files. I made a link <a> as a button.

<a class="resume-btn" href="./resumes/UX Resume.pdf"> UX </a>

I tried it on Firefox, Opera, and Chrome, it works fine.

Solution 2:[2]

If you just make a ordinary link to the file, the browser should not leave the page it is on, but just download the file straight away:

<a href="call_log.csv">Link</a>

I know it works for a .zip file, but haven't tried with .csv

But if you are having problems you can try zip the csv :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmed Barea
Solution 2 tmpethick