简体   繁体   English

如何使用Perl CGI以保留在浏览器中的格式显示文件内容

[英]How to display contents of a file with formatting preserved on the browser using Perl CGI

I have a text file saved in the server side. 我在服务器端保存了一个文本文件。 I have to read the file and display its content to the browser using perl CGI. 我必须阅读文件,并使用perl CGI将其内容显示在浏览器中。 The file has several paragraphs, newline and tab characters. 该文件具有几个段落,换行符和制表符。 I want to preserve those formatting. 我想保留这些格式。 Currently the contents of the file is displayed, but it is all jumbled up as a single continuous paragraph. 当前显示文件的内容,但是将其全部混为一个连续的段落。

print "<table border='1'>\n";
    print "<tr>\n";
    print "<td><b>Whole Report</b></td>\n";
    print "</tr>\n";
    print "<tr>\n";
    print "<td>$entireReport</td>\n";
    print "</tr>\n";
    print "</table>\n";

In the above code, I am reading the entire content of the file into the variable $entireReport. 在上面的代码中,我正在将文件的全部内容读入变量$ entireReport。

Thanks 谢谢

wrap the code in a <PRE></PRE> tag. 将代码包装在<PRE> </ PRE>标记中。 Say you have the contents in $fileContents then use: 假设您的内容在$ fileContents中,然后使用:

print "<td><pre>", CGI::escapeHTML($fileContents), "</pre></td>\n";

This will keep the formatting in the file verbatim. 这将使文件中的格式保持原样。

--dmg --dmg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM