'Get the file path where user downloaded a file

I currently have a function that prompts the user to download a JSON file:

    function downloadObjectAsJson (exportObj, exportName) {
      const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(exportObj))
      let downloadAnchorNode = document.createElement('a')
      downloadAnchorNode.setAttribute('href', dataStr)
      downloadAnchorNode.setAttribute('download', exportName + '.json')
      document.body.appendChild(downloadAnchorNode) // required for firefox
      downloadAnchorNode.click()
      downloadAnchorNode.remove()
    }

Is there a way to get the path the user selected to download this file to? Just need it to be displayed on the UI.

There are some API available that somehow allows access to the client file system like this, which lists down all the files in the selected directory:

async function listFilesInDirectory () {
      const dirHandle = await window.showDirectoryPicker()
      const promises = []
      for await (const entry of dirHandle.values()) {
        if (entry.kind !== 'file') {
          break
        }
        promises.push(entry.getFile().then((file) => `${file.name} (${file.size})`))
      }
      console.log(await Promise.all(promises))
   }

So I thought there might be some way to also get the path selected by the user when saving files.

Any other suggestions/means are welcome.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source