How to read a .txt file and convert to string in javascript? [duplicate]












0















This question already has an answer here:




  • Upload File as String to JavaScript Variable

    2 answers




I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.



    document.getElementById("answer").addEventListener(search);
document.getElementById("actualAns").addEventListener(search);

function myFunction() {
var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
if(n > -1) {
document.getElementById("result").innerHTML = "Correct";
} else {
document.getElementById("result").innerHTML = "Incorrect";
}
}









share|improve this question















marked as duplicate by str, Mohammad, charlietfl html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 18:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Upload File as String to JavaScript Variable

      2 answers




    I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.



        document.getElementById("answer").addEventListener(search);
    document.getElementById("actualAns").addEventListener(search);

    function myFunction() {
    var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
    var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
    var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
    if(n > -1) {
    document.getElementById("result").innerHTML = "Correct";
    } else {
    document.getElementById("result").innerHTML = "Incorrect";
    }
    }









    share|improve this question















    marked as duplicate by str, Mohammad, charlietfl html
    Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 19 '18 at 18:40


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Upload File as String to JavaScript Variable

        2 answers




      I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.



          document.getElementById("answer").addEventListener(search);
      document.getElementById("actualAns").addEventListener(search);

      function myFunction() {
      var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
      var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
      var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
      if(n > -1) {
      document.getElementById("result").innerHTML = "Correct";
      } else {
      document.getElementById("result").innerHTML = "Incorrect";
      }
      }









      share|improve this question
















      This question already has an answer here:




      • Upload File as String to JavaScript Variable

        2 answers




      I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.



          document.getElementById("answer").addEventListener(search);
      document.getElementById("actualAns").addEventListener(search);

      function myFunction() {
      var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
      var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
      var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
      if(n > -1) {
      document.getElementById("result").innerHTML = "Correct";
      } else {
      document.getElementById("result").innerHTML = "Incorrect";
      }
      }




      This question already has an answer here:




      • Upload File as String to JavaScript Variable

        2 answers








      javascript jquery html text






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 20:05









      dustinos3

      4551616




      4551616










      asked Nov 19 '18 at 18:36









      sparposparpo

      1918




      1918




      marked as duplicate by str, Mohammad, charlietfl html
      Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 19 '18 at 18:40


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by str, Mohammad, charlietfl html
      Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 19 '18 at 18:40


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can read the file like this:



          function readFile() {

          jQuery.get("file.txt", function(textString)) {

          //do what you want with the textString

          });
          }


          and in the textString you will have the string of text present in your file.






          share|improve this answer























          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 19 '18 at 23:32


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can read the file like this:



          function readFile() {

          jQuery.get("file.txt", function(textString)) {

          //do what you want with the textString

          });
          }


          and in the textString you will have the string of text present in your file.






          share|improve this answer























          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 19 '18 at 23:32
















          0














          You can read the file like this:



          function readFile() {

          jQuery.get("file.txt", function(textString)) {

          //do what you want with the textString

          });
          }


          and in the textString you will have the string of text present in your file.






          share|improve this answer























          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 19 '18 at 23:32














          0












          0








          0






          You can read the file like this:



          function readFile() {

          jQuery.get("file.txt", function(textString)) {

          //do what you want with the textString

          });
          }


          and in the textString you will have the string of text present in your file.






          share|improve this answer














          You can read the file like this:



          function readFile() {

          jQuery.get("file.txt", function(textString)) {

          //do what you want with the textString

          });
          }


          and in the textString you will have the string of text present in your file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 16:34

























          answered Nov 19 '18 at 18:47









          D. DianaD. Diana

          314




          314












          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 19 '18 at 23:32


















          • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
            – Nick
            Nov 19 '18 at 23:32
















          While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
          – Nick
          Nov 19 '18 at 23:32




          While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
          – Nick
          Nov 19 '18 at 23:32



          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory