从txt文件到网页的数据

时间:2018-09-05 15:56:56

标签: javascript jquery

所以我有一个包含一些数字的文本文件,我想在html页面中显示所有这些内容。我假设我需要使用javascript或jQuery。我尝试过类似的事情:

function getText() {
  fetch('inputfile.txt')
    .then(function(res) {
      return res.text();
    })
    .then(function(data) {
      console.log(data);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <title></title>
  <style type="text/css" media="screen">

  </style>
  <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
  <script type="text/javascript">
    $(function() {
      $.get('inputfile.txt', function(data) {
        //If the returned data is not empty or whitespace
        if (data) {
          // split the string into several strings when a new line occures
          var lines = data.split('\n'); // Take note of the \n This is the new line character also known as a line ending, end of line (EOL), or line break.
          // If there are more than 0 lines
          if (lines.length > 0) {
            // For every line console.log out what the line is with the line number prepended.
            for (var i = 0; i < lines.length; i++) {
              console.log(i + ": " + lines[i]);
            }
          }
        } else {
          // It is empty, do somethin else
          alert('no data present in txt file.');
        }
      });
    });
  </script>
</head>

<body>
</body>

</html>

但是每次我检查网页时,它都不会显示txt文件中的任何内容。

1 个答案:

答案 0 :(得分:0)

我在发布的代码中添加了注释,在“ pre”标签中添加了内容区域,但可以是任何内容,然后在收到响应的代码中将其加载到该标签/容器的html中:

try {

            Path dateFilePath = new Path(datesFile);
            FileSystem fileSystem = FileSystem.get(configuration);
            BufferedReader br = new BufferedReader(new InputStreamReader(fileSystem.open(dateFilePath)));
            String line;
            line = br.readLine();
            while (line != null) {
                if(todayDate.equals("0"))
                {
                    todayDate=line; 
                    //for two day Calculation
                    Dataset<Row> twoDayPriceDirDS=PriceDirectionCalculation(twoDayDs,todayDate, previousDate);
                    //for five day calculation

                    Dataset<Row> fiveDayPriceDirDS=PriceDirectionCalculation(fiveDayDs, todayDate, previousDate);

                    priceMovementForceDS=PriceMovementForceCalculation(twoDayPriceDirDS, fiveDayPriceDirDS);

                }
                else
                {

                    previousDate=todayDate;
                    todayDate=line;
                    //for two day Calculation
                    Dataset<Row> twoDayPriceDirDS=PriceDirectionCalculation(twoDayDs,todayDate, previousDate);

                    //for five day calculation
                    Dataset<Row> fiveDayPriceDirDS=PriceDirectionCalculation(fiveDayDs, todayDate, previousDate);

                    priceMovementForceDS=PriceMovementForceCalculation(twoDayPriceDirDS,fiveDayPriceDirDS);

                }
line = br.readLine();
}