【问题标题】:dynamically create download link for directory tree为目录树动态创建下载链接
【发布时间】:2011-06-10 20:00:13
【问题描述】:

我正在尝试使用 Codeigniter 作为平台基于文件夹结构和其中的各种文件创建目录树。我的视图工作正常(除了用于下载指定文件的文件链接。)我对 Jquery 和 java 非常陌生

<html>
<?PHP if ($_SESSION['profilepath'] != NULL) { ?>
<div id="files">
<?php //print_r($folders);?>
</div>
<script type="text/javascript">
$(document).ready(function() {
var files = <?php print_r(json_encode($folders)); ?>;
var file_tree = build_file_tree(files);
file_tree.appendTo('#files');

function build_file_tree(files) {

    var tree = $('<ul>');

    for (x in files) {

        if (typeof files[x] == "object") {
            var span = $('<span>').html(x).appendTo(
                $('<li>').appendTo(tree).addClass('folder')
            );
            var subtree = build_file_tree(files[x]).hide();
            span.after(subtree);
            span.click(function() {
                $(this).parent().find('ul:first').toggle();
            });

        } else {
            $('<li>').html(files[x]).appendTo(tree).addClass('file').click(function(){
                window.location=$(this).find("a").attr("href");return false;})
           //The click() function in the line above is where my links for download should be but I am unsure of what to do from here.

        }

    }

    return tree;

}
});    
</script>
</head>
<body>
<?PHP
} else {
$error = "Your user path is not set.";
  print_r($error);
}
?>
</body>
</html>

【问题讨论】:

    标签: php javascript jquery codeigniter


    【解决方案1】:

    嗯,我认为它更像是一个 php 的东西?您将链接设置为您的 php 脚本,将文件名作为变量传递

    href="download.php?file=folder\folder\folder\filename.ext"(可能想要散列/url编码)

    $file = (isset($_GET['file']))?$_GET['file']:"";
    

    检查一切是否正常等。然后强制它显示下载对话框

    if (!is_readable($file)) die('File not found or inaccessible!');
    
    $size = filesize($file);
    $name = rawurldecode($filename);
    $known_mime_types = array(
        "pdf"  => "application/pdf",
        "txt"  => "text/plain",
        "html" => "text/html",
        "htm"  => "text/html",
        "exe"  => "application/octet-stream",
        "zip"  => "application/zip",
        "doc"  => "application/msword",
        "docx" => "application/msword",
        "xls"  => "application/vnd.ms-excel",
        "xlsx" => "application/vnd.ms-excel",
        "ppt"  => "application/vnd.ms-powerpoint",
        "gif"  => "image/gif",
        "png"  => "image/png",
        "jpeg" => "image/jpg",
        "jpg"  => "image/jpg",
        "php"  => "text/plain"
    );
    
    $file_extension = strtolower(substr(strrchr($file, "."), 1));
    if (array_key_exists($file_extension, $known_mime_types)) {
        $mime_type = $known_mime_types[$file_extension];
    } else {
        $mime_type = "application/force-download";
    }
    
    
    @ob_end_clean(); //turn off output buffering to decrease cpu usage
    
    // required for IE, otherwise Content-Disposition may be ignored
    if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
    
    header('Content-Type: ' . $mime_type);
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    
    /* The three lines below basically make the
                download non-cacheable */
    header("Cache-control: private");
    header('Pragma: private');
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
    header("Content-Length: " . $size);
    
    $new_length = $size;
    
    $chunksize = 1 * (1024 * 1024); //you may want to change this
    $bytes_send = 0;
    /* output the file itself */
    $chunksize = 1 * (1024 * 1024); //you may want to change this
    $bytes_send = 0;
    if ($file = fopen($file, 'r')) {
        if (isset($_SERVER['HTTP_RANGE'])) fseek($file, $range);
    
        while (!feof($file) && (!connection_aborted()) && ($bytes_send < $new_length)) {
            $buffer = fread($file, $chunksize);
            print($buffer); //echo($buffer); // is also possible
            flush();
            $bytes_send += strlen($buffer);
        }
        fclose($file);
    } else die('Error - can not open file.');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多