Miqueel97

Untitled

Feb 19th, 2025
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. useEffect(() => {
  2.     const fetchData = async () => {
  3.       try {
  4.         const response = await fetch(`https://esb.optimalwayconsulting.com/fcbq/1/jR4rgA5K6Chhh5vyfrxo9wTScdg2NT7K/Federateds/getPhotobyUuidFederat/${player.uuid}`);
  5.         const base64Response = await response.text();
  6.         setImageUrl(base64Response);
  7.       } catch (error) {
  8.         console.error("Error al obtener la respuesta:", error);
  9.       }
  10.     };
  11.  
  12.     fetchData();
  13.  
  14.   }, [player.uuid]);
  15.  
  16.  
  17.   useEffect(() => {
  18.     // Decodificar la cadena Base64 a datos binarios
  19.     const binaryData = atob(imageUrl);
  20.  
  21.     // Crear un Uint8Array a partir de los datos binarios
  22.     const uint8Array = new Uint8Array([...binaryData].map(char => char.charCodeAt(0)));
  23.  
  24.     // Crear un Blob a partir del Uint8Array
  25.     const blob = new Blob([uint8Array]);
  26.  
  27.     // Crear una URL de objeto a partir del Blob
  28.     const imageUrlFromBlob = URL.createObjectURL(blob);
  29.  
  30.     // Hacer una solicitud fetch a la URL de la imagen para obtener la respuesta JSON
  31.     fetch(imageUrlFromBlob)
  32.       .then(response => response.json())
  33.       .then(data => {
  34.         // Extraer el valor de photoUrl de la respuesta JSON
  35.         const photoUrl = data.messageData[0].photoUrl;
  36.         setImageSrc(photoUrl);
  37.       })
  38.       .catch(error => {
  39.         console.error("Error al obtener la respuesta JSON:", error);
  40.       });
  41.  
  42.     // Limpiar la URL cuando el componente se desmonte
  43.     return () => URL.revokeObjectURL(imageUrlFromBlob);
  44.   }, [imageUrl]);
Add Comment
Please, Sign In to add comment