11

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.

3 Answers 3

10

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>

Sign up to request clarification or add additional context in comments.

3 Comments

I am moving to thanks page and starting download when user click start download button. Now to download file user again have to click "Download SO Logo". Can't we do any other way?
You can have only one page with two sections one for download presentation and one for 'thanks for downloading' which is initially hidden. On click you switch the displayed section. How you this is yet another question and there are multiple solutions: CSS, Javascript
Using download is the way to go unless you don't have to support silly browsers like IE11. In that case, setting the Content-Type to text/csv worked for me.
4

You can use the download attribute

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

This a HTML5 Attribute.

1 Comment

It seems that you are repeating an existing answer of the question. Please read this how-to-answer for providing a quality answer.
1

Using PHP, add this to your headers;

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/filepath/example.csv");

2 Comments

Well you can use the HTML 5 approach; <a href="example.csv" download>Click here</a> for downloading from a link. But with it being HTML5, I don't think you can rely on this working in older browsers.
@Mathew Lymer how can I implement this php code when I click a button?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.