1

I am using supabase as my storage, and i am making a request to get the response as CSV, as follows:

const exportColetasAsCSV = async ( IDs: number[] ) => {
const {data, error} = await supabase
    .from('coletas')
    .select()
    .in('id', IDs)
    .csv();

if (error) console.log('Erro exportColetasAsCSV:', error);
return data;
}

And i want to share it using expo-sharing (https://docs.expo.dev/versions/latest/sdk/sharing/), but to do that i need to have it as a local file, so i'm trying to save it locally with File-System (https://docs.expo.dev/versions/latest/sdk/filesystem/), but to save it locally i only see examples using http uri, like this:

FileSystem.downloadAsync(
    'http://techslides.com/demos/sample-videos/small.mp4',
    FileSystem.documentDirectory + 'small.mp4'
  )
  .then(({ uri }) => {
    Alert.alert(
      "App",
      uri
    );
})

Can i save it as a CSV like the one i receive as a response from supabase? If so, how?

1 Answers1

1

I searched around and was able to make it work! Here's how i made it:

const saveFile = async (data) => {

let directoryUri = FileSystem.documentDirectory;

let fileUri = directoryUri + "name.csv";

await FileSystem.writeAsStringAsync(fileUri, data, { encoding: FileSystem.EncodingType.UTF8 });

return fileUri;
};

const shareFile = async (fileUri) => {
const canShare = await Sharing.isAvailableAsync();

// Check if permission granted
if (canShare) {
  try{
    const res = await Sharing.shareAsync(fileUri);
    console.log('shareAsync', res);
    return true;
  } catch {
    return false;
  }
} else {
  alert("Você precisa dar permissão para Compartilhar.")
}};
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65