简体   繁体   中英

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. This script needs to write out the CSS back to /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). I have investigated the --allow-file-access chromium switch but still am not sure if it allows write privilege.

So, my question is: How can I write a string to a file using javascript that is running within the browser? 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. This switch re-enables file:// for testing.

--allow-file-access-from-files

By default, file:// URIs cannot read other file:// URIs. 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 . There you can read / write files (I think)

update

For chrome you may create a NaCL-extension . There is a flag to disable the snadbox for NaCL extenations: --allow-nacl-file-handle-api

PS: I think a browser would be the hardest runtime environment to write files because:

  1. There is (or a least was) no file-API
  2. The file API has no access to the host filesystem
  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 . These types of SDK's should have file I/O api's

tide SDK looks interesting.

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. 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 .

The files generated are stored in Chrome app-data. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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