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 :: how to keep scrolling with javascript 
Javascript :: module build failed (from ./node_modules/css-loader/dist/cjs.js): 
Javascript :: injected stylesheet remove 
Javascript :: adonis attach 
Javascript :: express get url parameters 
Javascript :: regex for 4 digit number javascript 
Javascript :: iterate array javascript 
Javascript :: asyncstorage react native 
Javascript :: nodejs create buffer from string 
Javascript :: concurrently script 
Javascript :: vuejs send required props to dynamic component 
Javascript :: javascript css left 
Javascript :: redirect via javascript 
Javascript :: redirect to homepage javascript 
Javascript :: js get distinct values from array 
Javascript :: javascript get x,y point on circle 
Javascript :: binary search in js 
Javascript :: js trigger mouseover 
Javascript :: how to find factorial of a number using Recursion in javascript 
Javascript :: set bg image in react 
Javascript :: how to add font family in material ui 
Javascript :: how to update the object value of any array key based on value 
Javascript :: javascript adding delay 
Javascript :: on uncheck checkbox hide button jquery 
Javascript :: get random numbers javascript 
Javascript :: local storage size check 
Javascript :: axios configure base url 
Javascript :: flutter convert json string to json 
Javascript :: javascript get device 
Javascript :: js test if string 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =