Get URL file content

Introduction

Implemented with JavaScript, given a URL, get the content of this file.

Code

fetch(url)
     .then(response => response.text())
     .then(content => {
       console.log(content);
     })
     .catch(error => {
       console.error('Error:', error);
     });

Note that cross domain issues may occur.

If the URL is an image

fetch(imageURL)
       .then(response => response.blob())
       .then(blob => {
         var reader = new FileReader();
         reader.onloadend = function () {
           console.log('Image base64:', reader.result)
         }
         reader.readAsDataURL(blob);
       })
       .catch(error => {
         console.error('Error:', error);
       });

Comments