4

Used Filesaver.js plugins to download a text file. By default file download in My download folder. Need to store the downloaded file in a specific folder.

7
  • 2
    It is not possible in javascript. But, you can change the download directory in your browser. Commented Apr 3, 2019 at 7:08
  • 1
    This setting is controlled by the user of the browser. It's not your right to go saving stuff wherever you like on the user's device. The device belongs to the user and they can choose where downloads are saved. In fact, JavaScript does not even know what the filesystem structure is (and nor should it, that would be a big security hole) Commented Apr 3, 2019 at 7:10
  • 1
    @yash vadhvani what do you mean? JavaScript cannot choose which folder to save a file into, as far as I know...unless you can provide an example?? Commented Apr 3, 2019 at 7:12
  • 1
    @ADyson I am really sorry i want to add via browser in my answer like before downloading you can set the download location Commented Apr 3, 2019 at 7:16
  • 1
    @ADyson Yeah that is perfectly true Sir. Commented Apr 3, 2019 at 8:40

2 Answers 2

10

It is not possible... fortunately! Imagine what would happen if JS which runs in your browser could change your filesystem. The security hole would be so big that everyone would (and definitely should) stop using the Internet. Imagine a situation where I've built a website which onload fires the code which save a file in your filesystem. The file lands in your cron.daily directory (suppose you use Linux). What is the file doing? - you may ask (if you knew that it's been even saved :smiling_imp:). Nothing special - just looking for some private data and when finished it deletes random files from /usr/bin, /proc, /sys and maybe /etc - just to see what happens.

Do you see the problem now? The code which runs in your browser before you can react to this cannot have such power to save anything in your filesystem. The only thing you can do to give the user a file is to use a module like Filesaver.js which, in fact, does not have access to user's file system at all. It just makes a GET request to the file directly and it's the browser which downloads the file (because that's how a browser works). So the only way you can change the location of the downloaded file is to change the browser settings. No other way I know of.

Sign up to request clarification or add additional context in comments.

1 Comment

If only a browser could provide a "select folder" API that (after user selects folder in the dialog displayed by the browser) gives JavaScript full write access to this folder...
4

The answer is No, changing a directory is not possible due to security reasons in the File API.

https://github.com/eligrey/FileSaver.js/issues/42

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.