1// As with JSON, use the Fetch API & ES6
2fetch('something.txt')
3 .then(response => response.text())
4 .then(data => {
5 // Do something with your data
6 console.log(data);
7 });
1const fileUrl = '' // provide file location
2
3fetch(fileUrl)
4 .then( r => r.text() )
5 .then( t => console.log(t) )
6
1const fileList = event.target.files;
2let fileContent = "";
3
4const fr = new FileReader();
5fr.onload = () => {
6 fileContent = fr.result;
7 console.log('Commands', fileContent);
8}
9
10fr.readAsText(fileList[0]);
1fetch('file.txt')
2 .then(response => response.text())
3 .then(text => console.log(text))
4 // outputs the content of the text file
5
1function readImage(file) { //function readImage(file) {
2 // Check if the file is an image.
3 if (file.type && !file.type.startsWith('image/')) {
4 console.log('File is not an image.', file.type, file);
5 return;
6 }
7
8 const reader = new FileReader();
9 reader.addEventListener('load', (event) => {
10 img.src = event.target.result;
11 });
12 reader.readAsDataURL(file);
13} 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);}