强制下载生成的HTML [英] Force Download of generated HTML

查看:169
本文介绍了强制下载生成的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个WordPress插件,当按下按钮时会生成HTML页面。这样可行;虽然代码在WP插件中,但问题与WordPress无关。下一步是给用户提示下载/打开创建的文件。



根据此处和其他地方的研究,此过程应创建下载/打开提示:

  / *另一个进程创建一个HTML文件somefile.html,并在插件中存储
夹;这是用fopen / fwrite / fclose完成的。
此过程由插件设置页面上的按钮启动。
单击该按钮时,将创建somefile.html。
所以此时,HTML文件被创建并存储在插件文件夹中* /

$ size = filesize($ thefile);
header('Content-Description:File Transfer');
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='somefile.html');
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);
退出;

该过程(通过点击插件设置页面上的按钮启动)确实创建了HTML文件,它被正确存储在插件文件夹中(文件位置不是问题,将在最终版本中解决)。然后我看到打开/保存文件对话框。



如果我从服务器打开生成的HTML文件,HTML就是预期的。但是如果我通过打开/保存提示打开生成的文件,我会得到插件设置页面的表示,而不是生成的HTML。



我怀疑我需要一个obflush()/ flush(),但将这些语句放在标题行之前并不能解决问题。



所以我的问题是打开/另存为对话框没有阅读存储在服务器上的'somefile.html'。我得到一个带有打开对话框的插件设置HTML页面。



如何确保通过打开/保存对话框打开我创建的HTML文件?



(请注意,尽管代码在WordPress插件中,但问题并非特定于WordPress。代码只是创建一个表单按钮;在提交时,表单操作会创建一个HTML文件,将其保存到服务器。然后使用'header'语句创建保存/打开对话框。)



已添加



此代码应显示该过程。这是用于创建somefile.html文件的有效HTML页面。

 <!DOCTYPE html PUBLIC -  / / W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns =http://www.w3 .org / 1999 / xhtml>< head>< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>< / head> 
< body>
这是包含一些内容的页面。它将创建HTML页面('xoutput'内容)。
< / body>
<!---以上是最初显示的页面 - >
<?php
//现在我们创建生成/保存文件的内容
$ xoutput ='<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns =http://www.w3.org/1999/xhtml> ;< head>< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>< / head>
< body>';
$ xoutput。='此处生成了一些内容。实际内容无关紧要。';
$ xoutput。='< / body> < / HTML>;
$ thefile =outputfile.html;
$ handle = fopen($ thefile,w);
fwrite($ handle,$ xoutput);
fclose($ handle);
$ quoted = sprintf('%s',addcslashes(basename($ thefile),'\\'));
$ size = filesize($ thefile);
//现在已经创建并存储了somefile.html,让我们创建打开/保存对话框
header('Content-Description:File Transfer');
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。$ quoted);
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);

退出;
返回;

当你加载页面时,你会得到'这里有一些内容生成'的HTML页面,而不是' somefile.html'content。

解决方案

新尝试和测试:

  if(file_exists($ thefile)){
header('内容描述:文件传输');
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。basename($ thefile));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header('Content-Length:'。filesize($ thefile));
ob_clean();
flush();
readfile($ thefile);
退出;
}


I am creating a WordPress plugin that will generate HTML page when a button is pressed. That works; the issue is not WordPress-related, although the code is in a WP plugin. The next step is to give the user a prompt to download/open the file that was created.

Based on research here and elsewhere, this process should create a download/open prompt:

   /* another process creates an HTML file "somefile.html", and stores it
 in the plugin folder; that is done with an fopen/fwrite/fclose.
 This process is started by a button on the plugin settings page.
 When the button is clicked, the "somefile.html" is created. 
So at this point, the HTML file is created and stored in the plugin folder */

    $size   = filesize($thefile);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='somefile.html'); 
    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);
    exit;

The process (started by the button click on the plugin settings page) does create the HTML file, and it is stored properly in the plugin folder (file location is not an issue and will be addressed in the final version). Then I see the open/save file dialog.

If I open the generated HTML file from the server, the HTML is as expected. But if I open the generated file via the open/save prompt, I get a representation of the plugin settings page, not the generated HTML.

I suspect that I need an obflush()/flush(), but placing those statements before the header lines does not fix the problem.

So my issue is that the open/save as dialog does not read the 'somefile.html' that was stored on the server. I get a plugins settings HTML page with the open dialog.

How do I ensure that the HTML file that I create is opened via the open/save dialog?

(Note that although the code is inside a WordPress plugin, the problem is not specific to WordPress. The code just creates a form button; on submit the form action creates an HTML file and saves it to the server. The 'header' statements are then used to create a save/open dialog.)

ADDED

This code should show the process. This is the 'effective' HTML page used to create the somefile.html file.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
    <body>
    This is the page with some content. It will create the HTML page (the 'xoutput' content).  
    </body>
<!--- the above is the page that is initially displayed -->
    <?php
     // now we create the content of the generated/saved file    
    $xoutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
    <body>';
    $xoutput .=  'There is some content generated here. Actual content doesn't matter.';
    $xoutput .=  '</body> </html>';
        $thefile = "outputfile.html";
        $handle = fopen($thefile, "w");
        fwrite($handle, $xoutput);
        fclose($handle);
    $quoted = sprintf('"%s"', addcslashes(basename($thefile), '"\\'));
    $size   = filesize($thefile);
    // now that the somefile.html has been created and stored, let's create the open/save dialog
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $quoted); 
    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);

        exit;
        return;

When you load the page, you get the 'There is some content generated here' HTML page, not the 'somefile.html' content.

解决方案

Freshly Tried And Tested:

if (file_exists($thefile)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($thefile));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: '.filesize($thefile));
    ob_clean();
    flush();
    readfile($thefile);
    exit;
}

这篇关于强制下载生成的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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