Html 在网页上的 .txt 文件中显示数据的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4642681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:03:42  来源:igfitidea点击:

simple way to display data in a .txt file on a webpage?

htmliframe

提问by DonG

Working on a project, one of the webpages will display a list of people (specifically, a list of people from a graduation class that haven't been located yet). Instead of manually updating these lists in tables which is a boneheaded Web 1.0 way of doing it, I'd like to take the submitted list of names, convert them to a simple .txt list, and then display that list on the webpage.

在处理一个项目时,其中一个网页将显示人员列表(特别是尚未找到的毕业班人员列表)。我不想在表格中手动更新这些列表,这是一种笨拙的 Web 1.0 方法,我想获取提交的名称列表,将它们转换为简单的 .txt 列表,然后在网页上显示该列表。

So far, the easist way to do this is to use an iframe element... only thing is, I cannot (or don't know how to) apply any text styling to the contents of the iframe. I've published a sample of what I've been able to accomplish here: http://dongarber.com/test//helpus-iframetest.html

到目前为止,最简单的方法是使用 iframe 元素……唯一的问题是,我不能(或不知道如何)将任何文本样式应用于 iframe 的内容。我在这里发布了我能够完成的示例:http: //dongarber.com/test//helpus-iframetest.html

The default font is courier, and the client probably ain't gonna be too keen on it. Is there a better way to do this, that's doesn't require ASP.NET or a database?

默认字体是 courier,客户可能不会太热衷于它。有没有更好的方法来做到这一点,不需要 ASP.NET 或数据库?

#list  p {
    font: arial;
    font-size: 14px;
}

...


    <p>Help us locate all of our classmates from the High School class of 1961. If  you know&nbsp;where they live&nbsp;or their e-mail addresses contact the Reunion Committee.</p>
    <p>&nbsp;</p>
    <div id="list"><p><iframe src="missingmen.txt" width=200 height=400 frameborder=0 ></iframe></p></div>



  </div>

采纳答案by thirtydot

Easy way:

简单的方法:

  • Rename missingmen.txtto missingmen.html.
  • Add a single line to the top of missingmen.html:
    <link href="txtstyle.css" rel="stylesheet" type="text/css" />
  • Create a file called txtstyle.css, and add to it a line like this:
    html, body {font-family:Helvetica, Arial, sans-serif}
  • 重命名missingmen.txtmissingmen.html.
  • 在 的顶部添加一行missingmen.html
    <link href="txtstyle.css" rel="stylesheet" type="text/css" />
  • 创建一个名为 的文件txtstyle.css,并向其添加如下一行:
    html, body {font-family:Helvetica, Arial, sans-serif}

回答by CLANofCIRE

I find that if I try things that others say do not work, it's how I learn the most.

我发现如果我尝试别人说行不通的事情,那是我学到最多的方法。

<p>&nbsp;</p>
<p>README.txt</p>
<p>&nbsp;</p>
<div id="list">
  <p><iframe src="README.txt" frameborder="0" height="400"
      width="95%"></iframe></p>
</div>

This worked for me. I used the yellow background-color that I set in the stylesheet.

这对我有用。我使用了在样式表中设置的黄色背景颜色。

#list  p {
    font: arial;
    font-size: 14px;
    background-color: yellow ;
}

回答by woot586

If you just want to throw the contents of the file onto the screen you can try using PHP.

如果您只想将文件的内容扔到屏幕上,您可以尝试使用 PHP。

<?php
    $myfilename = "mytextfile.txt";
    if(file_exists($myfilename)){
      echo file_get_contents($myfilename);
    }
?>


回答by nikolay_turpitko

In more recent browsers code like below may be enough.

在更新的浏览器中,像下面这样的代码可能就足够了。

<object data="https://www.w3.org/TR/PNG/iso_8859-1.txt" width="300" height="200">
Not supported
</object>

回答by David

How are you converting the submitted names to "a simple .txt list"? During that step, can you instead convert them into a simple HTML list or table? Then you could wrap that in a standard header which includes any styling you want.

您如何将提交的名称转换为“简单的 .txt 列表”?在该步骤中,您能否将它们转换为简单的 HTML 列表或表格?然后你可以将它包装在一个标准标题中,其中包含你想要的任何样式。

回答by holalluis

That's the code I use:

这是我使用的代码:

<?php   
    $path="C:/foopath/";
    $file="foofile.txt";

    //read file contents
    $content="
        <h2>$file</h2>
            <code>
                <pre>".htmlspecialchars(file_get_contents("$path/$file"))."</pre>
            </code>";

    //display
    echo $content;
?>

Keep in mind that if the user can modify $path or $file (for example via $_GET or $_POST), he/she will be able to see all your source files (danger!)

请记住,如果用户可以修改 $path 或 $file(例如通过 $_GET 或 $_POST),他/她将能够看到您的所有源文件(危险!)

回答by Daveo

You cannot style a text file, it must be HTML

您不能设置文本文件的样式,它必须是 HTML

回答by Shneor

You can add it as script file. save the txt file with js suffix

您可以将其添加为脚本文件。保存后缀为js的txt文件

in the head section add

在头部部分添加

<script src="fileName.js"></script>