Download a CSV document

Question:
How to download automatically a csv file into users navigator?

Product:
Wix Editor

What are you trying to achieve:
The idea is that the user click on a bottom that allows to download de csv file of a dataset! I already have my back end function on my own server that manages the data of dataset and retrieves a HTTPS response

I’m only been able to make it “downloadble” the document on users console

What have you already tried:

function downloadUsers() {
  const token = local.getItem('token');

  fetch("https://example.com/example", {
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${token}`
    }
  })
  .then((httpResponse) => {
    if (httpResponse.ok) {
      return httpResponse.blob();Blob
    } else {
      return Promise.reject("Hubo un problema con la descarga.");
    }
  })
  .then(blob => {
    
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onloadend = () => {
      const base64Data = reader.result; // data:text/csv;base64,...
      console.log(base64Data)
    };
  })
  .catch(error => {
    console.error('Error al descargar el archivo:', error);
  });
}

Additional information:
Thanks!

If your backend function returns a external URL with the CSV Document, you can use the wixLocationFrontend.to() method to allow users to download the file locally.

1 Like