Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the value of a variable converted to base64?

I am using a .zip file with thousands of geographical coordinates and converting to base64.

I can convert the file to base64, the problem is to save the result of this variable for later use.

I'm trying to use setState to save the variable's value but nothing happens.

Can you tell me what I'm doing wrong?

Here's my code I put in codesandbox

And here the zilFile I'm converting

  const [zipFile, setZipFile] = useState("");
  const [base64, setBase64] = useState("");

  const getBase64 = (file, cb) => {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {
      cb(reader.result);
    };
    reader.onerror = function (error) {};
  };

  const toBase64 = async () => {
    let auxZip = "";
    await getBase64(zipFile, (result) => {
      auxZip = result.substring(28);
      console.log("auxZip: ", auxZip);
      setBase64(auxZip);
    });
  };

  const onSave = () => {
    toBase64();

    console.log("base64: ", base64);
  };

  const handleZipChangle = (event) => {
    const file = event.target.files[0];
    setZipFile(file);
  };

enter image description here

like image 477
vbernal Avatar asked Nov 07 '22 05:11

vbernal


1 Answers

I have fixed like this, it worked perfectly, please take a look.

import React, { useState } from "react";
import "./styles.css";
import FormControl from "@material-ui/core/FormControl";
import Typography from "@material-ui/core/Typography";

export default function App() {
  const [base64, setBase64] = useState("");

  const getBase64 = (file, cb) => {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onloadend = (e) => {
      cb(e.target.result);
    };
    reader.onerror = function (error) {};
  };

  const onSave = () => {
    console.log("base64: ", base64);
  };

  const handleZipChangle = (event) => {
    const file = event.target.files[0];
    let auxZip = "";
    getBase64(file, (result) => {
      auxZip = result.substring(28);
      setBase64(auxZip);
    });
  };

  return (
    <div className="App">
      <FormControl>
        <Typography>Select Zip File:</Typography>
        <input
          accept="zip/*"
          type="file"
          id="contained-button-file"
          onChange={handleZipChangle}
        />
      </FormControl>
      <div style={{ marginTop: "30px" }}>
        <button onClick={onSave}>SAVE</button>
      </div>
    </div>
  );
}

or if you want to use zip file, you can use useEffect to check the state loaded and call getBase64

  useEffect(() => {
    let auxZip = "";
    zipFile &&
      getBase64(zipFile, (result) => {
        auxZip = result.substring(28);
        setBase64(auxZip);
      });
  }, [zipFile]);

  const onSave = () => {
    console.log("base64: ", base64);
  };

  const handleZipChangle = (event) => {
    const file = event.target.files[0];
    setZipFile(file);
  };
like image 95
Cuong DaoVan Avatar answered Nov 14 '22 21:11

Cuong DaoVan