[英]It it possible to use javascript to save a file to specific directory?
Used Filesaver.js plugins to download a text file. 使用Filesaver.js插件下载文本文件。 By default file download in My download folder.
默认情况下,在我的下载文件夹中下载文件。 Need to store the downloaded file in a specific folder.
需要将下载的文件存储在特定的文件夹中。
The answer is No, changing a directory is not possible due to security reasons in the File API.
答案是否定的,由于File API中的安全原因,无法更改目录。
https://github.com/eligrey/FileSaver.js/issues/42 https://github.com/eligrey/FileSaver.js/issues/42
It is not possible... fortunately! 不可能...幸运的是! Imagine what would happen if JS which runs in your browser could change your filesystem.
想象一下,如果在您的浏览器中运行的JS可以更改您的文件系统,将会发生什么。 The security hole would be so big that everyone would (and definitely should ) stop using the Internet.
安全漏洞将是如此之大,以至每个人都(并且绝对应该 )停止使用Internet。 Imagine a situation where I've built a website which
onload
fires the code which save a file in your filesystem. 试想一下,在那里我已经建立了一个网站,该网站的情况
onload
触发该文件保存在文件系统中的代码。 The file lands in your cron.daily
directory (suppose you use Linux). 该文件位于
cron.daily
目录中(假设您使用Linux)。 What is the file doing? 文件在做什么? - you may ask (if you knew that it's been even saved :smiling_imp:).
-您可能会问(如果您知道它已经被保存了: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. 没什么特别的-只是寻找一些私有数据,完成后它会从
/usr/bin
, /proc
, /sys
甚至是/etc
删除随机文件-只是看看会发生什么。
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.
你可以给用户一个文件的唯一事情是使用像Filesaver.js一个模块里面,其实,不必访问用户的文件系统在所有。 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).
它只是直接向文件发出GET请求,而由浏览器下载文件(因为这就是浏览器的工作方式)。 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.
我没有其他办法了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.