简体   繁体   English

使用纯 javascript 创建文件

[英]create a file using pure javascript

I Got this code to create a file using javascript.我得到了这段代码来使用 javascript 创建一个文件。

 function WriteToFile()
        { 
            var txt = new ActiveXObject("Scripting.FileSystemObject");
            var s = txt.CreateTextFile("11.txt", true);
            s.WriteLine('Hello');
            s.Close();
         }
WriteToFile();

it is working fine with IE but not Working in Chrome and Firefox.它适用于 IE,但不适用于 Chrome 和 Firefox。 usually the error is ReferenceError: ActiveXObject is not defined is there any alternate to create file in javascript, instead of ActiveXobject(Scripting.FileSystemObject") ?通常错误是ReferenceError: ActiveXObject is not defined是否有任何替代方法可以在 javascript 中创建文件,而不是ActiveXobject(Scripting.FileSystemObject")

This isn't possible.这是不可能的。 At least not in pure JavaScript anyway, ActiveX objects are part of the Windows/IE thing and are NOT in the ES6 spec ( http://www.ecma-international.org/ecma-262/6.0/index.html ), which is why they're not supported in Chrome or Firefox.至少不是在纯 JavaScript 中,ActiveX 对象是 Windows/IE 事物的一部分,不在 ES6 规范( http://www.ecma-international.org/ecma-262/6.0/index.html )中,其中这就是 Chrome 或 Firefox 不支持它们的原因。

If you wish to write JavaScript programs that can create files on the users local file system you're going to need to write a client side app, such as a chrome app ( https://developer.chrome.com/apps/api_index ) or write your program using Electron shell( http://electron.atom.io/ ).如果您希望编写可以在用户本地文件系统上创建文件的 JavaScript 程序,您将需要编写客户端应用程序,例如 Chrome 应用程序 ( https://developer.chrome.com/apps/api_index )或使用 Electron shell ( http://electron.atom.io/ ) 编写您的程序。

However, if what you want is a cross browser solution to create files at specific locations on a user file system, it's just not really natively possible.但是,如果您想要的是在用户文件系统的特定位置创建文件的跨浏览器解决方案,那么这在本机上是不可能的。

Sure browser plugins exist, but you'd need every user to install the plugin, so that's not exactly practical.确实存在浏览器插件,但是您需要每个用户都安装该插件,因此这不太实用。 For security reasons websites aren't allowed to read the users hard drive, and this is a good thing!出于安全原因,不允许网站读取用户硬盘,这是一件好事!

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

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