Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forcefully download .csv file instead of getting in open on browser in html

Tags:

html

csv

webpage

When I click download button, it link to new thanks webpage and after 2 sec start download .csv file from SD card.

Instead of downloading the file, it getting displayed on browser. Below is my code.

How can I get download option every time? Please no php code, as I am new to html.

I don't want to save using save as option after .csv file open browser.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>Monitoring System</title>
            <link href="/mchp.css" rel="stylesheet" type="text/css" />
            <script src="/mchp.js" type="text/javascript"></script>
        </head>

        <body>
            <div id="shadow-one">
                <div id="shadow-two">
                    <div id="shadow-three">
                        <div id="shadow-four">
                            <div id="page">
                                <div>
                                    <div>
                                        <div>
                                            <div id="title">    
                                                <div class="right">Interface</div>
                                                <div class="left">Overview</div>
                                            </div>

                                            <div id="content">
                                                <h1>Monitoring System</h1>

                                                <table style="padding-left: 10px;">
                                                    <div id="lcontent">
                                                    <div id="llogindiv" ><h1>Thanks for downloading...</h1></div>
                                                    <META HTTP-EQUIV="refresh" CONTENT="2;~GetEventFile~;";>
                                                </table>

                                            </div>
                                        </div>
                                        <div class="spacer">&nbsp;</div>
                                        <div id="footer">Copyright &copy; Monitoring System</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </body>
    </html>

~GetEventFile~ will give me file name at run time.

like image 330
Hari Avatar asked Jan 29 '16 09:01

Hari


2 Answers

At this point the safe solution is to set the response headers.

Modern browsers support download attribute for anchor elements which is used to specify the name of the file as it will be downloaded:

<a href="http://sstatic.net/stackexchange/img/logos/so/so-logo.png" download="logo.png">Download SO Logo</a>
like image 133
B0Andrew Avatar answered Nov 17 '22 09:11

B0Andrew


You can use the download attribute

<a href="Document.pdf" download="Document.pdf">Download the pdf</a>

This a HTML5 Attribute.

like image 27
Al Quarashi Avatar answered Nov 17 '22 08:11

Al Quarashi