单击链接弹出要下载的文件

时间:2014-03-19 05:05:00

标签: file download

我试图让.txt,.xml,.sql文件可以在同一页面下载,就像.zip,.docx文件一样,弹出并询问"你想下载这个文件吗? &#34 ;. 我已经研究过,发现这些文件的windows默认选项是在新浏览器中打开的,但我只想在同一页面下载。我已经附加了图像,就像点击那些.txt,.xml,.sql文件时需要弹出的那样。enter image description here 如何才能做到这一点?任何人都有这个想法。

enter image description here

3 个答案:

答案 0 :(得分:2)

        I have corrected by my own. Just putting the code below will successfully pop up any files to ask whether to download or not. This is my new code.
    The download.php contains the code below:

        <?php
        $filename = $_GET["file"];
        $contenttype = "application/force-download";
        header("Content-Type: " . $contenttype);
        header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
        if($_GET['path'] == 'new_uploads')
        readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename);
        else
        readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename);
        exit();
        ?>

And the file.php contains this code:

<img src="<?php echo base_url();?>assets/plupload/assessment_uploads/<?php echo $files->file_name?>" title="DOWNLOAD IMAGE" height="100" width="100" onClick="window.location.href='<?php echo base_url();?>logos/download?file=<?php echo $files->file_name?>&path=assessment_uploads'" class="download"/>

This will correctly sort out the current issue.

答案 1 :(得分:2)

You can use this and it will work like a charm . 
<?php
        $filename = $_GET["file"];
        $contenttype = "application/force-download";
        header("Content-Type: " . $contenttype);
        header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
        if($_GET['path'] == 'new_uploads')
        readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/new_uploads/".$filename);
        else
        readfile($_SERVER['DOCUMENT_ROOT']."50dl/admin_panel_new/assets/plupload/assessment_uploads/".$filename);
        exit();
        ?> 

答案 2 :(得分:1)

您应该在请求文件下载的代码中传递标题。

$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : ''; 
if ($task == 'download'){
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($_REQUEST['file_name']));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($_REQUEST['file_name']));
    ob_clean();
    flush();
    readfile($_REQUEST['file_name']);
    exit;}