React Native 应用程序将.xlsx文件保存到手机存储后自动退出

m3eecexj  于 2023-02-13  发布在  React
关注(0)|答案(1)|浏览(206)

我能够将.xlsx文件保存到手机存储中,但我有两个问题。

  • 应用程序将.xlsx文件保存到手机存储器后自动退出。
  • .xlsx文件保存在“DCIM”文件夹中,但我需要保存在“Download”文件夹中。
handleDownload = async () => {
const data = this.handleDownloadAbleLogs();

if (!data.length)
  return ToastAndroid.showWithGravity(
    "Sorry, no logs available to download.",
    ToastAndroid.LONG,
    ToastAndroid.CENTER
  );

const ws = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();

XLSX.utils.book_append_sheet(wb, ws, "Logs");

const wbout = XLSX.write(wb, {
  type: "base64",
  bookType: "xlsx",
});
const uri = `${FileSystem.cacheDirectory}logs.xlsx`;

try {
  await FileSystem.writeAsStringAsync(uri, wbout, {
    encoding: FileSystem.EncodingType.Base64,
  });
  console.log(uri);
  this.saveXLSXFile(uri);
  alert("Logs file has been saved in DCIM folder, do check there.");
} catch (e) {
  console.error(e);
}};

 saveXLSXFile = async (fileUri) => {
    const { status } = await MediaLibrary.requestPermissionsAsync();
    if (status === "granted") {
      const asset = await MediaLibrary.createAssetAsync(fileUri);
      await MediaLibrary.createAlbumAsync("Download", asset, false);
    } else alert("We need you permission to download this file.");   
};
pw9qyyiw

pw9qyyiw1#

它很简单的路径字符串更改为'../',在大多数Android手机图片和下载文件夹是在同一层次结构,所以这样做'../'将采取一步回来,并保存在下载文件夹

await MediaLibrary.createAlbumAsync("../Download", asset, false);

相关问题