如何强制下载 .csv 文件而不是在 html 浏览器中打开

新手上路,请多包涵

当我点击下载按钮时,它链接到新的感谢网页,2 秒后开始从 SD 卡下载 .csv 文件。

它没有下载文件,而是显示在浏览器上。下面是我的代码。

我怎样才能每次都获得下载选项?请不要 php 代码,因为我是 html 的新手。

我不想在 .csv 文件打开浏览器后使用另存为选项进行保存。

     <!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 将在运行时给我文件名。

原文由 Hari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 873
1 个回答

此时安全的解决方案是设置响应标头。

现代浏览器 支持 download 锚点 元素的属性,用于指定将要下载的文件的名称:

 <a href="http://sstatic.net/stackexchange/img/logos/so/so-logo.png" download="logo.png">Download SO Logo</a>

原文由 B0Andrew 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题