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.
-
2It is not possible in javascript. But, you can change the download directory in your browser.ArunKumar M N– ArunKumar M N2019-04-03 07:08:10 +00:00Commented Apr 3, 2019 at 7:08
-
1This 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)ADyson– ADyson2019-04-03 07:10:31 +00:00Commented 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??ADyson– ADyson2019-04-03 07:12:24 +00:00Commented 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 locationyash vadhvani– yash vadhvani2019-04-03 07:16:34 +00:00Commented Apr 3, 2019 at 7:16
-
1@ADyson Yeah that is perfectly true Sir.yash vadhvani– yash vadhvani2019-04-03 08:40:05 +00:00Commented Apr 3, 2019 at 8:40
2 Answers
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.
1 Comment
The answer is No, changing a directory is not possible due to security reasons in the File API.