【问题标题】:Download HTML page Using PHP and Ajax使用 PHP 和 Ajax 下载 HTML 页面
【发布时间】:2019-12-12 07:55:33
【问题描述】:

我想使用 php 和 ajax 下载 html 页面。现在我正在使用这样的链接:

<a href="https://www.example.com/page.html" download>Download</a>

有没有办法使用 php 和 ajax 来做到这一点?

提前感谢您的回答。

注意:我试过这个答案对我不起作用:How to download HTML using PHP?

【问题讨论】:

  • 这能回答你的问题吗? How to download HTML using PHP?
  • @Azzo,你想在用户点击下载按钮时将文件保存到用户的设备上吗,你说的下载是这个意思吗?
  • @OMiShah 是的,我想要这个。当用户单击按钮时,href="page.html" 应该下载他/她的设备。
  • 没有使用下载属性,文件正在下载吗?您目前面临什么问题
  • @OMiShah 您知道下载按钮在某些浏览器中不起作用。如果可能的话,我想用 php 让它更有用。

标签: php html


【解决方案1】:

您可以使用donwnload.js

示例代码:

Ajax 和包括 downloadjs

<script type="text/javascript" src="js/downloadjs.js"></script>
<script type="text/javascript"> 
     function dlHTML(){ 
        $.ajax({
        url: "yourhtmlpage.html", 
        success: download.bind(true, "text/html", "yourhtmlpagename.html")
    });
    }  
</script>

html

<button onclick="dlHTML()"></button>

【讨论】:

    【解决方案2】:

    希望对你有所帮助...

    <?php
    
         //Link to download file...
         $url = "http://example.com/test.php";
    
         //Code to get the file...
         $data = file_get_contents($url);
    
         //save as?
         $filename = "test.html";
    
         //save the file...
         $fh = fopen($filename,"w");
         fwrite($fh,$data);
         fclose($fh);
    
         //display link to the file you just saved...
         echo "<a href='".$filename."'>Click Here</a> to download the file...";
    
        ?>
    

    【讨论】:

    • 不,不是下载。我试过了。
    【解决方案3】:

    如果是安全 (https) 协议的 html 页面,您可以这样尝试

    $context = stream_context_create(
        array(
            "http" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
            )
        )
    );
    
    
    $contents = file_get_contents("https://www.example.com/page.html", false, $context);
    
    file_put_contents("download.html", $contents);
    

    【讨论】:

    • 不,不是下载。我试过了。
    • @Azzo 为我下载它,尝试使用有效的 html URL。否则我可以知道您是否有任何错误。
    • 没有错误,只是没有下载。没啥事儿。我正确使用了所有细节。
    【解决方案4】:

    当用户点击下载链接时,您可以强制下载。

    download.php 服务器上的文件

    <?php
    
    $fileName = $_GET['filename'];
    
    /* make sure to filter the file so that one could not download any other restricted file. Or better, keep your publicly downloadable files in a folder called "public_files" and store files in that */
    
    $filesFolder = "public_files";
    
    if (file_exists($filesFolder . "/" . $fileName)) {
    
        /* from https://stackoverflow.com/a/3476444/5882307 */
    
        $fileSize = filesize($filesFolder . "/" . $fileName);
    
        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: " . $fileSize);
        header("Content-Disposition: attachment; filename=" . $fileName);
    
        // Output file.
        readfile($filePath);
        exit();
    } else {
        die('The provided file path is not valid.');
    }
    ?>
    

    然后您可以将文件链接为:

    <a href="download.php?filename=yourfile.html">Download</a>
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2011-10-30
      • 2017-12-22
      • 2012-05-01
      • 1970-01-01
      • 2015-04-23
      相关资源
      最近更新 更多