简体   繁体   English

使用Javascript在Chrome和/或Firefox中写入文件

[英]Use Javascript to write to file within Chrome and/or Firefox

I'm creating a development tool that will be creating CSS based upon an SVG sprite sheet graphic file. 我正在创建一个开发工具,该工具将基于SVG Sprite Sheet图形文件创建CSS。 This script needs to write out the CSS back to /public/css/main.css. 该脚本需要将CSS写回到/public/css/main.css。 So, no, I'm not interested in a sandboxed file structure (though that is cool stuff). 因此,不,我对沙盒文件结构不感兴趣(尽管这很酷)。

Keep in mind that this is NOT for production. 请记住,这不是用于生产。 It is simply a development tool to streamline our workflow (and hopefully benefit the web design/developer community). 它只是简化我们的工作流程的开发工具(并有望使Web设计/开发人员社区受益)。 I have investigated the --allow-file-access chromium switch but still am not sure if it allows write privilege. 我研究了--allow-file-access铬开关,但仍不确定是否允许写入特权。

So, my question is: How can I write a string to a file using javascript that is running within the browser? 因此,我的问题是:如何使用浏览器中运行的javascript将字符串写入文件? Keeping in mind that this is not for production. 请记住,这不是用于生产。

Simple answer: 简单答案:

not possible due to security constraints of the browser. 由于浏览器的安全性限制而无法执行。

You can add flags like: 您可以添加如下标记:

--allow-file-access 

On ChromeOS, file:// access is disabled except for certain whitelisted directories. 在Chrome操作系统上,除了某些列入白名单的目录外,file://访问被禁用。 This switch re-enables file:// for testing. 此开关重新启用file://进行测试。

--allow-file-access-from-files

By default, file:// URIs cannot read other file:// URIs. 默认情况下,file:// URI无法读取其他file:// URI。 This is an override for developers who need the old behavior for testing. 对于需要旧行为进行测试的开发人员来说,这是一个替代。

But all these are for reading. 但是所有这些都是为了阅读。 I doubt you will succeed with this approach. 我怀疑您会采用这种方法成功。 MMaybe you should switch to node.js . MMaybe你应该切换到node.js There you can read / write files (I think) 在那里您可以读取/写入文件(我认为)

update 更新

For chrome you may create a NaCL-extension . 对于chrome,您可以创建NaCL-extension There is a flag to disable the snadbox for NaCL extenations: --allow-nacl-file-handle-api 有一个标志可以禁用snadbox进行NaCL扩展: --allow-nacl-file-handle-api allow --allow-nacl-file-handle-api

PS: I think a browser would be the hardest runtime environment to write files because: PS:我认为浏览器将是编写文件最困难的运行时环境,因为:

  1. There is (or a least was) no file-API 没有(或至少没有)文件API
  2. The file API has no access to the host filesystem 文件API无法访问主机文件系统
  3. The browser develop do their best to create a sandbox. 浏览器开发人员会尽力创建沙箱。

I don't think it's posible in chrome/firefox but I have seen webkit desktop application SDK's . 我认为这在chrome / firefox中不可行,但我已经看到了webkit桌面应用程序SDK的 These types of SDK's should have file I/O api's 这些类型的SDK应该具有文件I / O API

tide SDK looks interesting. 潮流SDK看起来很有趣。

If you're not making a Chrome/Firefox extension or desktop app (which, by what you said I'm sure you're not), there's no way you can do this with Javascript on the browser. 如果您没有制作Chrome / Firefox扩展程序或桌面应用程序(按照您所说的,我肯定不是),则无法在浏览器中使用Javascript进行此操作。 You should use a server-side language for something like this and maybe send the changes directly to the server, and then back to the client, but I don't think that fits your needs. 您应该将服务器端语言用于此类操作,并且可能将更改直接发送到服务器,然后再发送回客户端,但是我认为这不符合您的需求。

It might be worth your time to explore the File-System API . 您可能值得花时间探索File-System API

The files generated are stored in Chrome app-data. 生成的文件存储在Chrome应用数据中。 You cannot write to files elsewhere on the file-system. 您无法写入文件系统上其他位置的文件。 However, perhaps a script to grab the files in app-data and pull to another directory ran after generation might suffice? 然而,也许生成应用程序数据中的文件并将其拉到另一个目录后生成的脚本就足够了吗?

This is currently only available in a handful of browsers, Chrome is definitely included. 目前仅在少数浏览器中可用,Chrome绝对包含在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM