Inquiry for avoiding URL property use when uploading files in Prime React

UI Components for React
Post Reply
iyamus
Posts: 1
Joined: 09 Apr 2021, 18:10

09 Apr 2021, 18:12

I am trying to upload an excel file using fileupload in Prime React, then convert it into JSON type and display the result on the frond-side. The main concern is in Prime React, requires a URL like url="./upload" as a property, but in this scenario, it doesn't need to communicate with backend. Is there any workaround?

Code: Select all

<React.Fragment>
   <FileUpload
          mode="basic"
          accept=".xlsx"
          url="./upload"
          maxFileSize={1000000}
          label="Import"
          chooseLabel="Import"
          className="p-mr-2 p-d-inline-block"
          uploadHandler={readExcel}
          auto
          chooseLabel="Browse"
        />
   </React.Fragment>

Code: Select all

 const [rowCount, setRowCount] = useState(0);
  const [items, setItems] = useState([]);
  const readExcel = (event) => {
    const file = event.target.files[0];

    const promise = new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.readAsArrayBuffer(file);

      fileReader.onload = (e) => {
        const bufferArray = e.target.result;

        const wb = XLSX.read(bufferArray, { type: "buffer" });
        const wsname = wb.SheetNames[0];
        const ws = wb.Sheets[wsname];

        const data = XLSX.utils.sheet_to_json(ws);

        resolve(data);
      };
      fileReader.onerror = (error) => {
        reject(error);
      };
    });

    promise.then((d) => {
      console.log("check: ", d);
      setRowCount(d.length);
      setItems(d);
    });
  };

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests