FileSaver.saveAs save path change possible?

Viewed 31

I am working with react. I have a file to download. and its download to browser default save location(Download folder)

 FileSaver.saveAs(res.data, fileTitle + "." + fileExtension);

I have found that its not possible to change with FileSaver.saveAs method. So i want to know is there a another method in react to save file to local path.

1 Answers

You can't save a file to a particular path using FileSaver library.

But there's a new File System API you can use to achieve that. There's a project by googlechromelabs, a simple text editor (demo), designed to experiment with and demonstrate the new File System Access APIs.

Create a new file.

To save a file, call showSaveFilePicker(), which shows the file picker in "save" mode, allowing the user to pick a new file they want to use for saving

async function getNewFileHandle() {
  const options = {
    types: [
      {
        description: 'Text Files',
        accept: {
          'text/plain': ['.txt'],
        },
      },
    ],
  };
  const handle = await window.showSaveFilePicker(options);
  return handle;
}

Save changes to disk

// fileHandle is an instance of FileSystemFileHandle..
async function writeFile(fileHandle, contents) {
  // Create a FileSystemWritableFileStream to write to.
  const writable = await fileHandle.createWritable();
  // Write the contents of the file to the stream.
  await writable.write(contents);
  // Close the file and write the contents to disk.
  await writable.close();
}
Related