如何强制下载.csv文件,而不是在html中的浏览器中打开 [英] How to forcefully download .csv file instead of getting in open on browser in html

查看:142
本文介绍了如何强制下载.csv文件,而不是在html中的浏览器中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

不是下载文件,而是显示在浏览器上。以下是我的代码。

每次我如何获得下载选项?请不要使用PHP代码,因为我是html新手。



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

 <!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/xhtmlxml:lang =enlang =en>
< head>
< title>监控系统< / title>
< link href =/ mchp.css =stylesheettype =text / css/>
< script src =/ mchp.jstype =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>界面< / div>
< div class =left>概述< / div>
< / div>

< div id =content>
< h1>监控系统< / h1>

< table style =padding-left:10px;>
< div id =lcontent>
< div id =llogindiv>< h1>感谢您下载...< / h1>< / div>
< META HTTP-EQUIV =refreshCONTENT =2;〜GetEventFile〜;;>
< / table>

< / div>
< / div>
< div class =spacer>& nbsp;< / div>
< div id =footer>版权与复制;监控系统< / div>
< / div>
< / div>
< / div>
< / div>
< / div>
< / div>
< / div>
< / body>
< / html>

〜GetEventFile〜会在运行时为我提供文件名。

解决方案

在这一点上,安全的解决方案是设置响应头文件。

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



 < a href =http://sstatic.net/stackexchange/img/logos/so/so-logo.pngdownload = logo.png>下载SO Logo< / a>  

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.

解决方案

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>

这篇关于如何强制下载.csv文件,而不是在html中的浏览器中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆