“Lesen Sie Dateien in JavaScript” Code-Antworten

Lesen Sie die Datei 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);
  });
Kaotik

So lesen Sie eine Datei in JavaScript

fetch('file.txt')
  .then(response => response.text())
  .then(text => console.log(text))
  // outputs the content of the text file

Lesen Sie Dateien 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);}
CL

JavaScript -Datei lesen

<!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>
RemziStudios

Ähnliche Antworten wie “Lesen Sie Dateien in JavaScript”

Fragen ähnlich wie “Lesen Sie Dateien in JavaScript”

Weitere verwandte Antworten zu “Lesen Sie Dateien in JavaScript” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen