在浏览器中打开CSV文件,而不是下载HTML [英] Open CSV file in browser instead of being downloaded HTML

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

问题描述

只是想知道是否有一种方法可以在浏览器中打开csv文件而不是将其下载?

Just wondering if there is a way to open the csv file in the browser instead of being downloaded?

我的代码:

<a href="Myfile.csv">Open</a>

推荐答案

使用如下代码:<a href="Myfile.csv">Open</a>,您实际上没有进行任何下载.但是,如果客户端浏览器不具有读取/呈现文件(在本例中为.csv文件)的功能,它将强制用户下载文件.要明确执行下载,必须设置header以便告诉浏览器如何处理有关文件的请求.考虑一下这个过于简化的脚本:

With a Code like this: <a href="Myfile.csv">Open</a>, You are really not doing any download. However, if the Client Browser doesn't have the capability to read/render the File (in this case .csv File), it will force the user to download the file instead. To explicitly enforce a Download, you have to set the header so as to tell the Browser how to handle the Request for the File in Question. Consider this over-simplified Script:

PHP脚本:包含HTML标记

<?php   // NOTICE THAT THERE IS NO WHITE-SPACE OR OUTPUT BEFORE <?php
        // AND ALSO; NO "echo" STATEMENT AT ALL BEFORE THE if(isset()){} BLOCK.

        if(isset($_GET['d'])){
            $file = htmlspecialchars(trim($_GET['d']));
            processDownload($file);
        }

        function processDownload($fileName) {
            if($fileName){
                $dldFile    = $fileName;
                if(file_exists($fileName)){
                    $size       = @filesize($fileName);
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename=' . $fileName);
                    header('Content-Transfer-Encoding: binary');
                    header('Connection: Keep-Alive');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . $size);
                    return TRUE;
                }
            }
            return FALSE;
        }

    ?>


    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Download Example</title>
        </head>
        <body>
            <div class="container">
                <div class="col-md-12">
                    <!-- Myfile.csv IS IN THE SAME DIRECTORY AS THIS FILE: index.php -->
                    <!-- OTHERWISE; SET THE PATH TO THE CSV FILE AS VALUE OF d -->
                    <!-- NOTICE THAT THE LINK TO DOWNLOAD HERE IS THE SAME URL -->
                    <!-- WITH A QUERY PARAMETER ?d=Myfile.csv APPENDED TO IT.-->
                    <a href="index.php?d=Myfile.csv">Download CSV</a>
                </div>
            </div>
        </body>
    </html>

希望如此,这可能会为您提供以自己的方式进行操作的线索.

It is hoped, this might give you a Clue on how to go about it your own way.

祝你好运;-)

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

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