Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

read file javascript

// As with JSON, use the Fetch API & ES6
fetch('something.txt')
  .then(response => response.text())
  .then(data => {
  	// Do something with your data
  	console.log(data);
  });
Comment

javascript read file

<!DOCTYPE html>
<html>
  
<head>
    <title>Read Text File</title>
</head>
  
<body>
    <input type="file" name="inputfile"
            id="inputfile">
    <br>
   
    <pre id="output"></pre>
      
    <script type="text/javascript">
        document.getElementById('inputfile')
            .addEventListener('change', function() {
              
            var fr=new FileReader();
            fr.onload=function(){
                document.getElementById('output')
                        .textContent=fr.result;
            }
              
            fr.readAsText(this.files[0]);
        })
    </script>
</body>
  
</html>
Comment

Read files in JavaScript

function readImage(file) {  //function readImage(file) {
  // Check if the file is an image.
  if (file.type && !file.type.startsWith('image/')) {
    console.log('File is not an image.', file.type, file);
    return;
  }

  const reader = new FileReader();
  reader.addEventListener('load', (event) => {
    img.src = event.target.result;
  });
  reader.readAsDataURL(file);
} Check if the file is an image.  if (file.type && !file.type.startsWith('image/')) {    console.log('File is not an image.', file.type, file);    return;  }  const reader = new FileReader();  reader.addEventListener('load', (event) => {    img.src = event.target.result;  });  reader.readAsDataURL(file);}
Comment

js reading file

// get the file
fetch("path/to/file.extension")
	// get the text within the file
	.then((res) => res.text())
	// if you are reading from a json file:
	// .then((res) => res.json())
	// do something with the text
	.then((fileContents) => console.log(fileContents));
Comment

PREVIOUS NEXT
Code Example
Javascript :: react promises 
Javascript :: array javascript some vs every 
Javascript :: react native safeareaview 
Javascript :: JavaScript Number() Function 
Javascript :: math random js 
Javascript :: find js 
Javascript :: fetch get request 
Javascript :: sort in mongoose aggregate lookup 
Javascript :: hide div after 5 seconds vue js 
Javascript :: react-native-config 
Javascript :: javascript vector 
Javascript :: turnery opertaor js 
Javascript :: Map and Filter methods used together 
Javascript :: nodejs open file 
Javascript :: regex all 
Javascript :: reverse array javascript 
Javascript :: create directory when writing to file in nodejs 
Javascript :: react prevent form submission on enter key press inside inputs 
Javascript :: javascript insert text in textarea at cursor position 
Javascript :: url validation in formcontrol angular 8 
Javascript :: javascript default parameters 
Javascript :: how to seperate words seperated by commas using javascript 
Javascript :: get url parameters javascript 
Javascript :: jqery first img src 
Javascript :: github actions ssh 
Javascript :: react js classname with condition and normal 
Javascript :: javascript check if string contains a text substring 
Javascript :: from string to number js 
Javascript :: javascript splice 
Javascript :: jquery validation focus field on error 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =