Image URL to Base64 Conversion
# 🤝help
c
ii try to convert image form url
Copy code
javascript
axios.get('https://i.ibb.co/k1S3mfT/toincode.jpg', { responseType: 'arraybuffer' }).then((response) => {
  workflow.answer = Buffer.from(response.data).toString('base64')

})
and I get this error https://cdn.discordapp.com/attachments/1224002783204741230/1224002783611584552/image.png?ex=661be86c&is=6609736c&hm=e1e4fe1fedf06f56c70b588d81c5c471ebcf2faf82c3dd2eea5e24d8acf75ab1&
f
Hey there 👋 This code demonstrates how to convert an image URL to a Base64 string using JavaScript. It uses the
fetch
function to retrieve the image data from the specified URL and then converts it to a Base64 string 🙂
Copy code
javascript
fetch('https://i.natgeofe.com/n/4f5aaece-3300-41a4-b2a8-ed2708a0a27c/domestic-dog_thumb_square.jpg')
This line uses the
fetch
function to make an HTTP request to the specified URL, which is the URL of the image you want to convert to Base64. The
fetch
function returns a Promise that resolves with the response from the server.
Copy code
javascript
.then(response => response.blob())
This is a method chained to the Promise returned by
fetch
. It takes the response from the server and converts it to a Blob object using the
response.blob()
method. A Blob is a file-like object that represents raw binary data. This line returns a new Promise that resolves with the Blob object.
Copy code
javascript
.then(blob => {
  const reader = new FileReader();
  reader.onloadend = () => {
    workflow.answer = reader.result;
    console.log(workflow.answer);
  };
  reader.readAsDataURL(blob);
})
This is another method chained to the Promise returned by
response.blob()
. It takes the Blob object and does the following: 1.
const reader = new FileReader();
creates a new
FileReader
object, which is a built-in JavaScript object that can read the contents of Blob or File objects. 2.
reader.onloadend = () => { ... };
sets a callback function that will be executed when the reading operation is complete. 3.
workflow.answer = reader.result;
assigns the result of the reading operation to the `answer`variable that you already have in Botpress. The
reader.result
property contains the Base64-encoded string representation of the image data. 4.
console.log(workflow.answer);
logs the Base64 string to the console. 5.
reader.readAsDataURL(blob);
starts the reading operation on the Blob object. This method converts the Blob to a Base64-encoded string and stores the result in the
reader.result
property.
Copy code
javascript
.catch(error => {
  console.error('Error fetching image:', error);
});
This is a method chained to the Promise chain to handle any errors that may occur during the fetch or conversion process. If an error occurs, the callback function passed to
catch
will be executed, and the error will be logged to the console using
console.error
. You can replace the URL of the image with a variable in Botpress.
Copy code
javascript
fetch('workflow.URL')
This variable would contain a URL sent from a user. Here is the complete code, which you can copy into an execute code card in your flow and use.
Copy code
javascript
fetch('https://i.ibb.co/k1S3mfT/toincode.jpg')
  .then(response => response.blob())
  .then(blob => {
    const reader = new FileReader();
    reader.onloadend = () => {
      workflow.answer = reader.result
      console.log(workflow.answer)
    };
    reader.readAsDataURL(blob);
  })
  .catch(error => {
    console.error('Error fetching image:', error);
  });
q
f
You can add await
But you have to make it a function first
Copy code
javascript
async function fetchImage() {
  try {
    const response = await fetch('https://i.ibb.co/k1S3mfT/toincode.jpg');
    const blob = await response.blob();
    const reader = new FileReader();
    
    await new Promise((resolve) => {
      reader.onloadend = () => {
        workflow.answer = reader.result;
        console.log(workflow.answer);
        resolve();
      };
      reader.readAsDataURL(blob);
    });
  } catch (error) {
    console.error('Error fetching image:', error);
  }
}

await fetchImage();
c
thanks it's work
f
Happy to hear that
2 Views