简体   繁体   English

是否可以使用javascript将文件保存到特定目录?

[英]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.

相关问题 是否可以使用javascript将文件保存到当前文件所在的目录? - It it possible to use javascript to save a file to same directory as current file? 是否可以使用javascript在特定目录中保存诸如.swf,.mp3,.txt等文件,而无需“另存为”提示? - Is it possible to save a file such as .swf, .mp3, .txt, etc. without the “save as” prompt using javascript in a specific directory? 使用Javascript在文件路径中列出特定目录 - Use Javascript to list a specific directory in file path 将streamlit里面用Javascript生成的文件保存到一个目录下 - Save a file generated with Javascript inside streamlit to a directory 使用javascript获取文件目录 - Use javascript to get the directory of a file 使用CGI和Javascript保存文件 - Use CGI and Javascript to save a file Javascript生成文件并要求用户保存它,可能吗? - Javascript generate file and ask user to save it, possible? 是否可以将 $_SESSION 与 Javascript 文件一起使用? - Is it possible to use $_SESSION with a Javascript file? 点击是否可以包含特定的javascript文件? - Is it possible to include a specific javascript file on click? 是否可以使用Javascript加载特定的文件类型? - Is it possible to load a specific file type using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM